MVC斷點續傳

來源:互聯網
上載者:User

標籤:

jQuery-File-Upload 檔案地址

jQuery-File-Upload-master.zip

 

123456789101112131415161718192021222324 [HttpGet][HttpPost]public HttpResponseMessage Upload(){    // Get a reference to the file that our jQuery sent.  Even with multiple files, they will all be their own request and be the 0 index    HttpPostedFile file = HttpContext.Current.Request.Files[0];     // do something with the file in this space    // {....}    // end of file doing    this.SaveAs(HttpContext.Current.Server.MapPath("~/Images/") + file.FileName, file);     // Now we need to wire up a response so that the calling script understands what happened    HttpContext.Current.Response.ContentType = "text/plain";    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();    var result = new { name = file.FileName };          HttpContext.Current.Response.Write(serializer.Serialize(result));    HttpContext.Current.Response.StatusCode = 200;     // For compatibility with IE‘s "done" event we need to return a result as well as setting the context.response    return new HttpResponseMessage(HttpStatusCode.OK);}

 

  • 實現斷點續傳邏輯

        這其中主要是通過解析Http要求標頭中的Content-Range屬性來獲知此次處理的檔案片斷,後續就是基本的檔案操作了,沒什麼可說的。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 private void SaveAs(string saveFilePath, HttpPostedFile file){    long lStartPos = 0;    int startPosition = 0;    int endPosition = 0;    var contentRange=HttpContext.Current.Request.Headers["Content-Range"];    //bytes 10000-19999/1157632    if (!string.IsNullOrEmpty(contentRange))    {        contentRange = contentRange.Replace("bytes", "").Trim();        contentRange = contentRange.Substring(0, contentRange.IndexOf("/"));        string[] ranges = contentRange.Split(‘-‘);        startPosition = int.Parse(ranges[0]);        endPosition = int.Parse(ranges[1]);    }    System.IO.FileStream fs;    if (System.IO.File.Exists(saveFilePath))    {        fs = System.IO.File.OpenWrite(saveFilePath);        lStartPos = fs.Length;             }    else    {        fs = new System.IO.FileStream(saveFilePath, System.IO.FileMode.Create);        lStartPos = 0;    }    if (lStartPos > endPosition)    {        fs.Close();        return;    }    else if (lStartPos < startPosition)    {        lStartPos = startPosition;    }    else if (lStartPos > startPosition && lStartPos < endPosition)    {        lStartPos = startPosition;    }    fs.Seek(lStartPos, System.IO.SeekOrigin.Current);    byte[] nbytes = new byte[512];    int nReadSize = 0;    nReadSize = file.InputStream.Read(nbytes, 0, 512);    while (nReadSize > 0)    {        fs.Write(nbytes, 0, nReadSize);        nReadSize = file.InputStream.Read(nbytes, 0, 512);    }    fs.Close();          }

MVC斷點續傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.