当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Is it possible to have a progress bar on the client side linked to the progress of saveAs()?

问题描述:

I have a controller for uploading a file of any type. A condensed version of it is

 [HttpPost]

public ActionResult Index (HttpPostedFileBase file, string selectedOrgName, string selectedCatName, string previouslyUploadedFilesJSON = null)

{

// ...... bunch of stuff

try

{

file.SaveAs(newFileServerPathAndName); // Saves file with new file name in assets folder

}

catch

{

throw new Exception("Error when trying to save file to Assets folder.");

}

// ... bunch of other stuff

return View();

}

and the model is like

@using (Html.BeginForm("Index",

"FileUpload",

FormMethod.Post,

new { enctype = "multipart/form-data", @id ="upload-file-form"}))

{

<div>

<label for="file">Upload File:</label>

<input type="file" name="file" id="file" /><br>

<input type="submit" value="Upload" /><img class="loading-icon hidden" src="/ClientPortal/images/loadinggif.gif"/>

<input type="hidden" name="selectedOrgName" class="selectedOrgInput" />

<input type="hidden" name="selectedCatName" id="selectedCatInput"/>

<input type="hidden" name="previouslyUploadedFilesJSON" value="@ViewBag.uploadedFilesJSON" />

<br><br>

</div>

}

and what I'm wondering is whether it's possible for me to have a progress bar on the client's side that is linked to the progress of the saveAs function. In Google Chrome there is already something on the bottom of the browser window like

Uploading (31%)

when uploading the file, so there must be some way to tap into that number on the client side, or there must be some other way using the controller.

网友答案:

The easiest solution is to do this in the browser, since it obviously knows how much data has been sent. Your controller isn't invoked until the upload is complete, so that won't do it, and even if you had the data on the server side then you have to work out how to get the data back to the browser.

jQuery has some support for this, e.g. File upload progress bar with jquery

网友答案:

You should look into SignalR. We just implemented a progress bar for file uploads in an MVC application and it works great. It's really easy to implement and there are countless tutorials and samples on the web that will show you how to wire it up.

分享给朋友:
您可能感兴趣的文章:
随机阅读: