使用 Ajax.BeginForm 绑定 HttpPostedFileBase
我有一个表单,它使用 HttpPostedFileBase 的默认绑定器绑定模型和文件上传。
使用 Html.BeginForm() 时效果很好。但是,我想使用 AJAX 执行相同的操作,因此我将其替换为 Ajax.BeginForm() 并相应地更改参数。
该模型仍然正确绑定,但是我无法将文件上传绑定到 HttpPostedFileBase。
这绑定了模型和文件上传:
<% using (Html.BeginForm("MapUpdateColumns", "RepositoryAdmin", FormMethod.Post, new { id = "UpdateDataset", enctype = "multipart/form-data" })) {%>
这只绑定了模型:
<% using (Ajax.BeginForm("MapUpdateColumns", "RepositoryAdmin", new AjaxOptions { UpdateTargetId = "columnMappings" }, new { id = "UpdateDataset", enctype = "multipart/form-data" })) {%>
控制器操作:
public ActionResult MapUpdateColumns(DatasetViewModel model, HttpPostedFileBase sourceFile)
这应该可能吗?如果可以的话,我做错了什么?谢谢。
I have a form which binds a model and a file upload using the default binder for 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.
The model still binds correctly, however I can't get the file upload to bind to the 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法使用 AJAX 上传文件。实现此目的的一种方法是使用隐藏的 iframe,它将模拟 AJAX 调用并执行实际的文件上传或使用 Flash。这是一个非常好的 jQuery Form 插件,使用隐藏的 iframe,能够透明地 ajax 化包含文件字段的表单提交。
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 Form plugin using a hidden iframe which is capable of transparently ajaxifying a form submission containing file fields.
有可能,答案在这里:
https://stackoverflow.com/a/13522052/1067149
我自己做的并且保证它有效。
It is possible, the answer is here:
https://stackoverflow.com/a/13522052/1067149
I did it myself and it's guaranteed it works.
标签输入中添加
id="file"
在您的 ACTIONRESULT 参数中的
HttpPostedFileBase“文件”名称和视图标记名称应该相同,
这对我有用
ADD
id="file"
in your tag inputIN YOUR ACTIONRESULT PARAMETER
HttpPostedFileBase 'file' name and view tag name should be same
IT WORKS FOR ME
是的,我也同意。您绝对可以使用“Ajax.BeginForm”上传文件。将“enctype =“multipart/form-data””添加到 AjaxOptions 对象。
Yes I also agree. You can definately upload files using 'Ajax.BeginForm'.Add 'enctype = "multipart/form-data"' to the AjaxOptions object.