也许这只是我,但我正在尝试使用 jQuery 对话框来捕获用户想要上传的文件。 必须IE9支持。 IE9 不支持我遇到的许多示例和第 3 方工具中使用的 FormData 对象。因此,为了使我的请求不刷新整个页面,我必须将上传内容放入 iFrame 中吗?真的吗?我知道我可以获得一些 Flash uploader ,但我们的网站不支持 Flash,所以现在不可能。
拜托..拜托有人告诉我我做错了,还有一种更简单的方法,因为我似乎找不到它..至少不能在 IE9 上运行。
表单
<form action="/ControllerName/ActionName" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
然后像这样进入一个iFrame
<iframe src="myUrl"></iframe>
查看:
@using (Html.BeginForm("FileUpload","Message",FormMethod.Post,new { enctype="multipart/form-data"})) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Message</legend>
//your html here
<input type="file" name="files"/>
</fieldset>
Controller :
[HttpPost]
public ActionResult FileUpload(FormCollection values, IEnumerable<HttpPostedFileBase> files)
{
//do what you want with form values then for files
foreach (var file in files)
{
if (file.ContentLength > 0)
{
byte[] fileData = new byte[file.ContentLength];
file.InputStream.Read(fileData, 0, file.ContentLength);
//do what you want with fileData
}
}
}
我提供多个文件上传所以我用
IEnumerable<HttpPostedFileBase> files
如果你只需要一个文件,那么只使用
HttpPostedFileBase file
并在您的 View 中将输入类型更改为
<input type="file" name="file"/>
就这么简单。问候
编辑:
看看更多好的例子:
http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/
http://css.dzone.com/articles/implementing-html5-drag-drop
问候
我是一名优秀的程序员,十分优秀!