jQuery Ajax 文件上传 [英] jQuery Ajax File Upload
问题描述
我可以使用下面的 jQuery 代码通过 ajax 请求的 POST 方法执行文件上传吗?
Can I use the following jQuery code to perform file upload using POST method of an ajax request ?
$.ajax({
type: "POST",
timeout: 50000,
url: url,
data: dataString,
success: function (data) {
alert('success');
return false;
}
});
如果可以,我是否需要填写data
部分?这是正确的方法吗?我只将文件 POST 到服务器端.
If it is possible, do I need to fill data
part? Is it the correct way? I only POST the file to the server side.
我一直在谷歌搜索,但我发现的是一个插件,而在我的计划中我不想使用它.至少目前是这样.
I have been googling around, but what I found was a plugin while in my plan I do not want to use it. At least for the moment.
推荐答案
通过 AJAX 上传文件不能.
您可以使用IFrame
上传文件,无需刷新页面.
您可以在此处查看更多详细信息.
File upload is not possible through AJAX.
You can upload file, without refreshing page by using IFrame
.
You can check further details here.
XHR2 支持通过 AJAX 上传文件.例如.通过 FormData
对象,但不幸的是并非所有/旧浏览器都支持它.
With XHR2, File upload through AJAX is supported. E.g. through FormData
object, but unfortunately it is not supported by all/old browsers.
FormData
支持从以下桌面浏览器版本开始.
FormData
support starts from following desktop browsers versions.
- IE 10+
- Firefox 4.0+
- Chrome 7+
- Safari 5+
- Opera 12+
有关详细信息,请参阅 MDN 链接.
For more detail, see MDN link.
这篇关于jQuery Ajax 文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!