Click here to Skip to main content
15,891,002 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i Make this ASHX class :
C#
private void RangeDownload(string fullpath,HttpContext context)
   {
       long size,start,end,length,fp=0;
       using (StreamReader reader = new StreamReader(fullpath))
       {

           size = reader.BaseStream.Length;
           start = 0;
           end = size - 1;
           length = size;
           // Now that we've gotten so far without errors we send the accept range header
           /* At the moment we only support single ranges.
            * Multiple ranges requires some more work to ensure it works correctly
            * and comply with the spesifications: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2
            *
            * Multirange support annouces itself with:
            * header('Accept-Ranges: bytes');
            *
            * Multirange content must be sent with multipart/byteranges mediatype,
            * (mediatype = mimetype)
            * as well as a boundry header to indicate the various chunks of data.
            */
           context.Response.AddHeader("Accept-Ranges", "0-" + size);
           // header('Accept-Ranges: bytes');
           // multipart/byteranges
           // http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2

           if (!String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
           {
               long anotherStart = start;
               long anotherEnd = end;
               string[] arr_split = context.Request.ServerVariables["HTTP_RANGE"].Split(new char[] { Convert.ToChar("=") });
               string range = arr_split[1];

               // Make sure the client hasn't sent us a multibyte range
               if (range.IndexOf(",") > -1)
               {
                   // (?) Shoud this be issued here, or should the first
                   // range be used? Or should the header be ignored and
                   // we output the whole content?
                   context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
                   throw new HttpException(416, "Requested Range Not Satisfiable");

               }

               // If the range starts with an '-' we start from the beginning
               // If not, we forward the file pointer
               // And make sure to get the end byte if spesified
               if (range.StartsWith("-"))
               {
                   // The n-number of the last bytes is requested
                   anotherStart = size - Convert.ToInt64(range.Substring(1));
               }
               else
               {
                   arr_split = range.Split(new char[] { Convert.ToChar("-") });
                   anotherStart = Convert.ToInt64(arr_split[0]);
                   long temp = 0;
                   anotherEnd = (arr_split.Length > 1 && Int64.TryParse(arr_split[1].ToString(), out temp)) ? Convert.ToInt64(arr_split[1]) : size;
               }
               /* Check the range and make sure it's treated according to the specs.
                * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
                */
               // End bytes can not be larger than $end.
               anotherEnd = (anotherEnd > end) ? end : anotherEnd;
               // Validate the requested range and return an error if it's not correct.
               if (anotherStart > anotherEnd || anotherStart > size - 1 || anotherEnd >= size)
               {

                   context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
                   throw new HttpException(416, "Requested Range Not Satisfiable");
               }
               start = anotherStart;
               end = anotherEnd;

               length = end - start + 1; // Calculate new content length
               fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
               context.Response.StatusCode = 206;
           }
       }
           // Notify the client the byte range we'll be outputting
           context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
           context.Response.AddHeader("Content-Length", length.ToString());
           // Start buffered download
           //context.Response.WriteFile(fullpath, fp, length);
           long fileLength = length;
                   //context.Response.WriteFile(fullpath);
           using (FileStream fileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
           {
               try
               {

                   fileStream.Seek(fp, SeekOrigin.Begin);
                   Byte[] buffer = new Byte[10240];
                   // Send file to client.
                   while (fileLength > 0)
                   {
                       if (context.Response.IsClientConnected)
                       {
                           int lengthBuff = fileStream.Read(buffer, 0, 10240);

                           context.Response.OutputStream.Write(buffer, 0, lengthBuff);

                           context.Response.Flush();

                           fileLength = fileLength - lengthBuff;
                       }
                       else
                       {
                           fileLength = -1;
                       }
                   }
               }
               catch (HttpException ex)
               {
                   context.Response.StatusCode = ex.GetHttpCode();
               }
               finally
               {
                   fileStream.Close();
               }
           }
           context.Response.End();

   }

   public void ProcessRequest(HttpContext context)
   {
       //context.Response.Headers.Clear();
       string filename = context.Request["fn"];
       string mimetype = "video/mp4";
       string fullpath=("E:/"+filename);
       if (System.IO.File.Exists(fullpath))
       {

           context.Response.ContentType = mimetype;
           if (!String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
           {
               //request for chunk
               RangeDownload(fullpath,context);
           }
           else
           {
               //ask for all
                   //context.Response.WriteFile(fullpath);
                   using (FileStream fileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)){
                     try
                     {
                         long fileLength = fileStream.Length;
                         context.Response.AddHeader("Content-Length", fileLength.ToString());
                         Byte[] buffer = new Byte[10240];
                         // Send file to client.
                         while (fileLength > 0)
                         {
                             if (context.Response.IsClientConnected)
                             {
                                 int length = fileStream.Read(buffer, 0, 10240);

                                 context.Response.OutputStream.Write(buffer, 0, length);

                                 context.Response.Flush();

                                 fileLength = fileLength - length;
                             }
                             else
                             {
                                 fileLength = -1;
                             }
                         }
                     }
                     catch (HttpException ex)
                     {
                         context.Response.StatusCode = ex.GetHttpCode();
                     }
                     finally
                     {
                         fileStream.Close();
                     }

                 }

           }
       }
       else
       {
           throw new HttpException(404, "Video Not Found Path:"+fullpath);
       }
   }

   public bool IsReusable {
       get {
           return false;
       }
   }


and then i call it in mediaelement in WPF like this :
C#
mediaElement.Source = new Uri("http://localhost:3402/VideoStreamer.ashx?fn=a.mp4");
              mediaElement.Play();


But my problem is it not seekable and i can't change position my video.
Where is My Problem ?
Thank You.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900