使用 Ajax.BeginForm 绑定 HttpPostedFileBase

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2491230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:52:02  来源:igfitidea点击:

Binding HttpPostedFileBase using Ajax.BeginForm

asp.net-mvcajaxmodel-binding

提问by TonE

I have a form which binds a model and a file upload using the default binder for HttpPostedFileBase.

我有一个表单,它使用 HttpPostedFileBase 的默认绑定器绑定模型和文件上传。

This works fine when using Html.BeginForm(). However, I wanted to perform the same action using AJAX so I replaced this with Ajax.BeginForm() changing the parameters accordingly.

这在使用 Html.BeginForm() 时工作​​正常。但是,我想使用 AJAX 执行相同的操作,因此我将其替换为 Ajax.BeginForm() 相应地更改了参数。

The model still binds correctly, however I can't get the file upload to bind to the HttpPostedFileBase.

该模型仍然可以正确绑定,但是我无法将文件上传绑定到 HttpPostedFileBase。

This binds the model and the file upload:

这将绑定模型和文件上传:

<% using (Html.BeginForm("MapUpdateColumns", "RepositoryAdmin", FormMethod.Post, new { id = "UpdateDataset", enctype = "multipart/form-data" })) {%>

This only binds the model:

这仅绑定模型:

<% using (Ajax.BeginForm("MapUpdateColumns", "RepositoryAdmin", new AjaxOptions { UpdateTargetId = "columnMappings" }, new { id = "UpdateDataset", enctype = "multipart/form-data" })) {%>

The controller action:

控制器动作:

public ActionResult MapUpdateColumns(DatasetViewModel model, HttpPostedFileBase sourceFile)

Should this be possible, and if so what am I doing wrong? Thanks.

这应该是可能的,如果是这样,我做错了什么?谢谢。

回答by Darin Dimitrov

You cannot upload files with AJAX. One way to achieve this is to use a hidden iframe which will simulate an AJAX call and perform the actual file upload or use Flash. Here's a very nice jQuery Formplugin using a hidden iframe which is capable of transparently ajaxifying a form submission containing file fields.

您无法使用 AJAX 上传文件。实现此目的的一种方法是使用隐藏的 iframe,它将模拟 AJAX 调用并执行实际的文件上传或使用 Flash。这是一个非常好的jQuery 表单插件,它使用隐藏的 iframe,它能够透明地对包含文件字段的表单提交进行 ajaxifying。

回答by Demian Flavius

It is possible, the answer is here:

有可能,答案在这里:

https://stackoverflow.com/a/13522052/1067149

https://stackoverflow.com/a/13522052/1067149

I did it myself and it's guaranteed it works.

我自己做的,它保证它有效。

回答by Taha Baig

ADD id="file"in your tag input

ADDid="file"在你的标签输入

IN YOUR ACTIONRESULT PARAMETER HttpPostedFileBase 'file' name and view tag name should be same

在你的 ACTIONRESULT PARAMETER HttpPostedFileBase 'file' name 和 view tag name 应该相同

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(tbl_products tbl_products,HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                tbl_products.phototype = file.ContentType;
                tbl_products.photo =new byte[file.ContentLength ];
                file.InputStream.Read(tbl_products.photo,0, file.ContentLength);

                if(obj.insert(tbl_products))
                {
                return RedirectToAction("Index");
                }
                else
                {
                    return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
                }   
            }

            return View(tbl_products);
        }

IT WORKS FOR ME

这个对我有用

回答by Arnold.Krumins

Yes I also agree. You can definately upload files using 'Ajax.BeginForm'.Add 'enctype = "multipart/form-data"' to the AjaxOptions object.

是的,我也同意。您肯定可以使用'Ajax.BeginForm'.Add 'enctype = "multipart/form-data"' 上传文件到AjaxOptions 对象。