码迷,mamicode.com
首页 > Web开发 > 详细

MVC断点续传

时间:2016-08-16 23:36:57      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

jQuery-File-Upload 文件地址

jQuery-File-Upload-master.zip

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[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属性来获知此次处理的文件片断,后续就是基本的文件操作了,没什么可说的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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断点续传

标签:

原文地址:http://www.cnblogs.com/jack-zeng/p/5778197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!