HttpPostedFile在文件上传过程中为空,如果我使用AJAX

编程入门 行业动态 更新时间:2024-10-28 06:26:00
本文介绍了HttpPostedFile在文件上传过程中为空,如果我使用AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是用我的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

本文发布于:2023-10-12 20:42:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为空   过程中   文件上传   HttpPostedFile   AJAX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!