遇到调用控制器Post方法的麻烦 [英] Having troubles calling a Controller Post Method

查看:160
本文介绍了遇到调用控制器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);
    }

从此actionscript方法发布:

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.

感谢,

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.

是View中需要阅读的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天全站免登陆