调用控制器 Post 方法时遇到问题 [英] Having troubles calling a Controller Post Method

查看:20
本文介绍了调用控制器 Post 方法时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的方法

    [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");

        }

    }

但是当 encodeAndSave 方法运行时,没有文件被保存到服务器......

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.

谢谢,
马特

推荐答案

我遇到了 [很多] 麻烦,最后我使用了这个;

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.

我遗漏的最重要的事情是需要阅读的视图中的 Form 方法

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" }))   { %>

enctype 允许您获取所选文件的文件名和路径,并使控制器能够获取输入流.没有它,我发现 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.

这篇关于调用控制器 Post 方法时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆