有麻烦调用一个控制器POST方法控制器、麻烦、方法、POST

2023-09-08 12:01:03 作者:没了你老娘照样活

下面是我的方法

    [AcceptVerbs(HttpVerbs.Post)]
    public void SaveImage(FormCollection formValues)
    {
        byte[] contents = Convert.FromBase64String(Request.Form["file"]);
        System.IO.File.WriteAllBytes(Server.MapPath(Request.Form["name"]), contents);
    }

这是越来越张贴到这个动作方法:

It is getting posted to from this actionscript method:

    public function encodeAndSave(e:MouseEvent = null):void
	{
		var date:Date = new Date();
		var by:ByteArray = PNGEnc.encode(canvas.main_bdata);
		var req:URLRequest = new URLRequest(server_path+"Home/SaveImage");
		var params:URLVariables = new URLVariables();
		params.file = Base64.encodeByteArray(by);
		params.name = "MyImage.png";
		req.method = URLRequestMethod.POST;
		req.data = params;
		var ldr:URLLoader = new URLLoader(req);

		ldr.addEventListener(Event.COMPLETE, complete);

		ldr.load(req);

		function complete(e:Event):void
		{
			navigateToURL(new URLRequest("?" + Math.random()), "_self");

		}

	}

但EN codeAndSave方式运行时,没有任何文件被保存到服务器...

But when the encodeAndSave method runs, no file gets saved to the server...

有谁知道如何判断该SaveImage方法,甚至跑了?此外,当我键入: http://www.mysite.com/Home/SaveImage 到地址栏就写着资源无法找到。

Does anyone know how to tell if the SaveImage method has even ran? Also, when I type: http://www.mysite.com/Home/SaveImage into the address bar it says "The resource cannot be found".

任何人有任何想法,为什么会做这样或我能做些什么来揣摩一下吗?

Anyone have any ideas as to why it would be doing this or what i can do to try to figure it out?

如果您需要任何更多信息,请让我知道,我会更新我的问题。

If you need any more information please let me know and I'll update my question.

谢谢, 马特

Thanks, Matt

推荐答案

我有麻烦[许多]这点,我用这个到底;

I had a [lot] of trouble with this and I used this in the end;

查看

<% using (Html.BeginForm( "PhotoAdd", "Photos", FormMethod.Post, new { enctype = "multipart/form-data" }))   { %>

<input type="file" id="file" name="file" class="field" style="width:300px;"/>

<%} %>

控制器

var file = Request.Files["file"];
byte[] buf = new byte[file.ContentLength];
file.InputStream.Read(buf, 0, file.ContentLength);

我不知道这是否是你要找的,但就像我说我有很多问题,让我的用户上传自己的照片在我的网站。

I'm not sure whether this is what you are looking for but like I said I have a lot of problems allowing my users to upload a photo of themselves in my web site.

基本上,我有一个观点,其中包含类型为文件的领域上和提交按钮后返回活动。

Basically I have a View which contains a field of type "file" on it and a "Submit" button for the post back event.

然后在控制器我有以下;

Then in the controller I have the following;

    [AcceptVerbs( HttpVerbs.Post ), Authorize]
    public ActionResult PhotoAdd(FormCollection collection)
    {
        try
        {
            var file = Request.Files["file"];

            byte[] buf = new byte[file.ContentLength];
            file.InputStream.Read(buf, 0, file.ContentLength);

我遇到的困难越来越控制器获得请求连同文件名和路径。有一次,我,我可以将文件转换为字节数组,我可以把它保存到一个SQL数据库和图像领域。

The difficulty I ran into was getting the controller to get the request along with the file name and path. Once I had that I could convert the file to an array of bytes that I could then save to a SQL Database and an Image field.

一个最大的事情,我缺少的是在需要读取查看窗体的方法

The one biggest thing I was missing was the Form method in the View which needs to read

<% using (Html.BeginForm( "PhotoAdd", "Photos", FormMethod.Post, new { enctype = "multipart/form-data" }))   { %>

该加密类型,您可以拿起所选文件的文件名和路径和控制器能够抓住InputStream的。如果没有它,我发现,无论是Request.Files是空的,或者,它仅包含文件名,因此不能打开它。

The enctype allows you to pick up the filename and path of the selected file and for the controller to be able to grab the inputstream. Without it I found that either Request.Files was empty or that it only contained the file name and hence could not open it.

一旦保存图片到你的数据库,显示它是一个轻而易举。

Once you have saved the image to your database, displaying it is a doddle.

我希望这回答了你的问题。

I hope this answers your question.