zoukankan      html  css  js  c++  java
  • 断点续传

    基于Range

    using System;
    using System.IO;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
     
    namespace Video3dApp.Controllers
    {
        public class Video3dController : Controller
        {
     
            public ActionResult BrandUSA() {
                return View();
            }
     
            public ActionResult Detail() {
                return View();
            }
            
            [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post | HttpVerbs.Options)]
            public void ReadVideo() {
                var reqRange = Request.Headers["Range"];
                string[] reqBlockRange = null;
                if ( !string.IsNullOrEmpty(reqRange) ) {
                    reqBlockRange = reqRange.Replace("bytes=", "").Split('-');
                    Response.StatusCode = 206;
                    Response.AddHeader("status", "206");
                }
     
                Response.AddHeader("accept-ranges", "bytes");
                Response.AddHeader("access-control-allow-methods", "HEAD, GET, OPTIONS");
                Response.AddHeader("access-control-allow-origin", "*");
                Response.AddHeader("cache-control", "public, max-age=30726563");
                Response.AddHeader("content-disposition", $"attachment;  filename=test.mp4");
                Response.ContentType = "video/mp4";
     
                string fileName = Server.MapPath("/UploadFiles/test.mp4");
     
                using ( var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) )
                using (var reader=new BinaryReader(stream)) {
                    long fileSize = stream.Length;
     
                    long startPosition = 0;
                    long partialSize = fileSize;
                    if ( reqBlockRange != null ) {
                        startPosition = Convert.ToInt32(reqBlockRange[0]);
                        partialSize = fileSize - startPosition;
                    }
     
                    //Read partial content into the buffer with a specified size
                    byte[] buffer = new byte[(int)partialSize];
                    // go to offset address 
                    reader.BaseStream.Seek(startPosition, SeekOrigin.Begin);
     
                    // fill buffer from starting at address to address + BlockSise
                    reader.Read(buffer, 0, (int)partialSize);
                    Response.AddHeader("content-range", $"bytes {startPosition}-{startPosition + partialSize - 1}/{fileSize}");
                    Response.AddHeader("Content-Length", $"{partialSize}");
                    Response.BinaryWrite(buffer);
                }
            }
        }
    }
  • 相关阅读:
    驼峰命名法
    Java中有关Null的9件事
    java开发中JDBC连接数据库代码和步骤
    TestNG 与 Junit的比较
    洛谷 P1880 [NOI1995]石子合并(区间dp,断环为链)
    洛谷 P1901 发射站(单调栈)
    洛谷 P2947 [USACO09MAR]向右看齐Look Up(单调栈)
    洛谷 P2629 好消息,坏消息(前缀和,单调队列,断环为链)
    洛谷 P3810 【模板】三维偏序(陌上花开)(CDQ分治)
    洛谷 CF429D Tricky Function(平面最近点对,分治)
  • 原文地址:https://www.cnblogs.com/kunlunmountain/p/10018466.html
Copyright © 2011-2022 走看看