gpt4 book ai didi

ajax - 使用 MVC 3 ajax 样式上传文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:24:34 25 4
gpt4 key购买 nike

也许这只是我,但我正在尝试使用 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

问候

关于ajax - 使用 MVC 3 ajax 样式上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10802816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com