从javascript上传文件时如何使用最大带宽

时间:2016-09-26 07:14:13

标签: javascript multithreading asp.net-mvc-4 file-upload

我尝试使用XMLHttpRequest将来自JavaScript的chunked文件上传到Aps.net MVC应用程序,其中每个块的大小为5MB。首先尝试按顺序上传每个块(在完成一个块之后再取另一个块)。 Sequntial Upload image

每个要上传的部分需要5到7秒。

然后我按顺序上传到并行(一次发送3个块),但大约需要16到19秒。 Parallel upload image

其中3个块同时开始,但几乎同时结束,但它与Sequntial * 3相同。

服务器端代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="item" type="checkbox" name="vehicle"  value="bike" >I have a bike <br>

JavaScript代码:

var upload_request = new XMLHttpRequest();                 var prev_Loaded = 0;                 chunk = self.file [self.slice_method](chunk_part.range_start,chunk_part.range_end);

 [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class FileManagerController : Controller
{
    /// <summary>
    /// this method used to upload part file.
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [ValidateInput(false)]
    public async Task<ActionResult> UploadPartFile()
    {
        var temppath = Server.MapPath("~/Temp");

        try
        {
            await Task.Run(() =>
            {

                string FileName = HttpUtility.UrlDecode(Request.Headers["X-FILE-NAME"]);
                string ContentRange = Request.Headers["Content-Range"];
                using (var fileStream = new FileStream(Path.Combine(temppath, FileName), FileMode.OpenOrCreate))
                {
                    Request.InputStream.CopyTo(fileStream);
                }
            });



        }
        catch (Exception exc)
        {

        }
        return Json(temppath, JsonRequestBehavior.AllowGet);
    }
}

如何提高文件上传速度?

0 个答案:

没有答案