javascript - Ajax web api 文件上传带进度条

标签 javascript jquery ajax asp.net-web-api

我正在尝试使用这段代码将文件上传到服务器,

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 .

enter image description here

此插件还允许您通过公开 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/

相关文章:

javascript - 防止 contenteditable 在 ENTER 上添加 <div> - Chrome

javascript - 如何在 AJAX 中高效地编写 "toggle database value"函数?

javascript - block 元素移除时平滑过渡,纯JS

javascript - d3 Sankey 分配固定的 x,y 位置

javascript - 如何在不使用自定义触发器的情况下使用 jQuery 监听插入到 DOM 中的元素?

javascript - 在数组中存储 Highcharts 问题

java - 部署时出现 502 代理错误

javascript - SuiteCRM 内联编辑字段未保存

javascript - 参数类型的限制

javascript - 如何从剑道网格中获取选中的行?