我是用我的asp MVC项目文件上传功能。 它的工作很大,直到我开始使用我的网页上一些AJAX功能。
I am using file upload functionality in my asp 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'); }); });
还要注意的HttpPostedFileBase代替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
发布评论