结合HttpPostedFileBase使用Ajax

结合HttpPostedFileBase使用Ajax

本文介绍了结合HttpPostedFileBase使用Ajax.BeginForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结合模型和文件上传使用默认的粘合剂HttpPostedFileBase。一个形式

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

本使用Html.BeginForm时正常工作()。不过,我想执行使用AJAX,所以我代替这与Ajax.BeginForm()相应地改变参数相同的动作。

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.

该模式仍然绑定正确,但我不能得到的文件上传到绑定到HttpPostedFileBase。

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

这只能绑定模型:

<% 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.

推荐答案

您不能上传文件与AJAX。实现这一目标的一个方法是使用一个隐藏的iframe,这将模拟一个AJAX调用和执行实际文件上载或使用Flash。下面是使用一个隐藏的iframe,它能够透明地ajaxifying表单提交包含文件中的字段的jQuery表插件,一个非常漂亮的

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.

这篇关于结合HttpPostedFileBase使用Ajax.BeginForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!