繁体   English   中英

使用进度条的Ajax web api文件上载

[英]Ajax web api File Upload With Progress bar

我正在尝试将此代码上传到服务器,

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!");
    }
}
}

在Web Api控制器中

[HttpPost]
[ActionName("UploadFile")]
public HttpResponseMessage UploadFile([FromRoute]string SomeStringData)
{
    // Save the uploaded file here on the server
}

文件上传完美,我的问题是如何显示进度条,我正在使用jquery mobile进行设计。

我怎么能用百分比或其他东西显示进度条?

你试过blueimp的jQuery文件上传吗? 我在我的一些项目中使用它,它确实为您提供了一个进度条以及您希望在文件上传中拥有的许多其他功能。

基本版本(以及所有其他版本)都有一个文件上传进度条。 你可以查看一个演示

在此输入图像描述

此插件还允许您通过公开fileuploadprogress函数来修改进度条和相关信息 - 阅读文档

$('#fileupload').bind('fileuploadprogress', function (e, data) {
    // Log the current bitrate for this upload:
    console.log(data.bitrate);
});

此插件使用Bootstrap的进度条组件来显示其进度条。

更好的是它提供了与ASP.NET的文档集成 ,并且还有一个github项目,用于如何将此插件与ASP.NET MVC一起使用。

希望这对你有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM