我正在尝试使用这段代码将文件上传到服务器,
HTML:
<input type="file" id="file1" name="browsefile" multiple="multiple" accept="video/mp4,video/*">
JavaScript:
function FileUpload(SomestringParameter) {
var files = $("#file1").get(0).files;
if (files.length > 0) {
if (window.FormData !== undefined) {
var data = new FormData();
for (i = 0; i < files.length; i++) {
data.append("file" + i, files[i]);
}
$.ajax({
type: "POST",
url: "http://localhost:50443/UploadFile/" + SomestringParameter,
contentType: false,
processData: false,
data: data,
success: function (results) {
alert(results);
for (i = 0; i < results.length; i++) {
alert(results[i]);
}
}
});
}
else {
alert("This browser doesn't support HTML5 multiple file uploads!");
}
}
}
在网络 API Controller 中,
[HttpPost]
[ActionName("UploadFile")]
public HttpResponseMessage UploadFile([FromRoute]string SomeStringData)
{
// Save the uploaded file here on the server
}
文件已完美上传,我的问题是如何显示进度条,我正在使用 jquery mobile 进行设计。
如何显示带有百分比或其他内容的进度条?
最佳答案
你试过了吗blueimp's jQuery File Upload ?我在我的几个项目中使用过它,它确实为您提供了一个进度条以及您希望在文件上传中拥有的许多其他功能。
基本版本(以及所有其他版本)都有一个文件上传进度条。您可以查看 demo .
此插件还允许您通过公开 fileuploadprogress
函数来修改进度条和相关信息 - Read Documentation
$('#fileupload').bind('fileuploadprogress', function (e, data) {
// Log the current bitrate for this upload:
console.log(data.bitrate);
});
此插件使用 Bootstrap's progress bar组件显示其进度条。
更好的是它提供了一个 documented integration使用 ASP.NET 并且有一个 github project了解如何将此插件与 ASP.NET MVC 一起使用也是。
希望对你有帮助。
关于javascript - Ajax web api 文件上传带进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21604279/