使用Bootstrap进度条实现MVC文件上传

18

有没有一种简单的方法,在文件加载时显示一个阻塞的Bootstrap进度条?

在Chrome中,随着文件上传,进度会在状态栏中显示:

The progress is shown in chrome as the file is uploaded

我希望对话框看起来像这样 this

enter image description here

我的操作看起来像这样:

 [HttpPost]
        public ActionResult Upload(UploadViewModel model)
        {
                using (MemoryStream uploadedFile = new MemoryStream())
                {
                    model.File.InputStream.CopyTo(uploadedFile);                            
                    uploadService.UploadFile(uploadedFile, model.File.ContentType)
                    return View();
                 }
         }

模型:

  public class UploadViewModel
    {
        [Required]
        public HttpPostedFileBase File { get; set; }
    }

查看:

@model Bleh.Web.Models.UploadViewModel

@using (Html.BeginForm("Upload", "Home",
  FormMethod.Post, new { enctype = "multipart/form-data", @role = "form" }))
{
   <div class="form-group">
    @Html.LabelFor(m => m.File)
    @Html.TextBoxFor(m => m.File, new { type = "file", @class = "form-control" })
    <strong>@Html.ValidationMessageFor(m => m.File, null, new { @class = "label label-danger" })</strong>
</div>

<div class="form-group noleftpadding">
    <input type="submit" value="Upload File" class="btn btn-primary" />
</div>
}

有没有一种简单的方法来处理浏览器显示的百分比并将其应用于进度条?


1
你能展示一下你是如何在视图中使用这个函数的吗? - MoonKnight
2个回答

38

ajax进度处理程序是否起作用?

function uploadFile(){
    myApp.showPleaseWait(); //show dialog
    var file=document.getElementById('file_name').files[0];
    var formData = new FormData();
    formData.append("file_name", file);
    ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", progressHandler, false);
    ajax.addEventListener("load", completeHandler, false);
    ajax.open("POST", "/to/action");
    ajax.send(formData);
}

function progressHandler(event){
    var percent = (event.loaded / event.total) * 100;
    $('.bar').width(percent); //from bootstrap bar class
}

function completeHandler(){
    myApp.hidePleaseWait(); //hide dialog
    $('.bar').width(100);
}
注意:myApp.showPleaseWait();myApp.hidePleaseWait();在OP提供的链接中有定义。
(编辑:之前formData和formdata不一致)

你如何在视图中使用它?能否提供一个例子? - MoonKnight
视图采用 Twitter Bootstrap 链接。我在我的帖子中添加了对话框的显示/隐藏功能。 - vusan
处理进度条的最简单方法。谢谢@vusan - Chaminda Bandara

0

对于那些仍在寻找另一种解决方案的人,这里有一个:

function showPleaseWait() {

    if (document.querySelector("#pleaseWaitDialog") == null) {
        var modalLoading = '<div class="modal" id="pleaseWaitDialog" data-backdrop="static" data-keyboard="false" role="dialog">\
        <div class="modal-dialog">\
            <div class="modal-content">\
                <div class="modal-header">\
                    <h4 class="modal-title">Please wait...</h4>\
                </div>\
                <div class="modal-body">\
                    <div class="progress">\
                      <div class="progress-bar progress-bar-success progress-bar-striped active" role="progressbar"\
                      aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width:100%; height: 40px">\
                      </div>\
                    </div>\
                </div>\
            </div>\
        </div>\
    </div>';
        $(document.body).append(modalLoading);
    }

    $("#pleaseWaitDialog").modal("show");
}

/**
 * Hides "Please wait" overlay. See function showPleaseWait().
 */
function hidePleaseWait() {
    $("#pleaseWaitDialog").modal("hide");
}
 $('#load').click(function () {
    showPleaseWait();
});

然后像这样调用:

<button id="load">Load It!</button>

如果你想要隐藏它,请确保在成功后调用hidePleaseWait()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接