HttpPostedFile在文件上传过程中为空,如果我使用AJAX [英] HttpPostedFile in File Upload process is NULL if I use AJAX

查看:194
本文介绍了HttpPostedFile在文件上传过程中为空,如果我使用AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用我的asp.net MVC项目文件上传功能。 它的工作很大,直到我开始使用我的网页上一些AJAX功能。

I am using file upload functionality in my asp.net MVC project. It worked great until I started using some AJAX functionality on my page.

该HttpPostedFile始终是空的Ajax页面。

The HttpPostedFile is always NULL on Ajax page.

随着AJAX调用我的页面上怎样才能解决这个问题呢?

How can solve this issue along with calling ajax on my page?

推荐答案

由于你不能上传使用AJAX文件,我建议你优秀的的jQuery表格插件,让您ajaxify表单,并支持文件上传的。幕后插件生成一个隐藏的iframe来处理上传和完全透明的你:

Because you cannot upload files using AJAX I would recommend you the excellent jquery form plugin which allows you to ajaxify your forms and supports file uploads. Behind the scenes the plugin generates a hidden iframe to handle the upload and is completely transparent to you:

<form id="myForm" action="/home/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file" id="file" />
    <input type="submit" value="upload" />
</form>

控制器:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
    // TODO: handle the file here
    return PartialView("success");
}

最后ajaxify形式:

And finally ajaxify the form:

$(function() {
    $('#myForm').ajaxForm(function(result) {
        alert('thank you for uploading');
    });
});

还要注意的<一个用法href="http://msdn.microsoft.com/en-us/library/system.web.httppostedfilebase.aspx"><$c$c>HttpPostedFileBase代替<一个href="http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.aspx"><$c$c>HttpPostedFile在控制器动作。作为一个抽象类,这将简化您的单元测试。

Also notice the usage of HttpPostedFileBase instead of HttpPostedFile in the controller action. Being an abstract class this will simplify your unit tests.

这篇关于HttpPostedFile在文件上传过程中为空,如果我使用AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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