c# - Resuming downloading -


the idea simple, creating service user can put direct link of file being hosted on website , program open stream remote server , start reading file in bytes , return each readed byte user.

so far managed working , here code

    public void index()     {         //create stream file         stream stream = null;          //this controls how many bytes read @ time , send client         int bytestoread = 10000; //10000          // buffer read bytes in chunk size specified above         byte[] buffer = new byte[bytestoread];          // number of bytes read         try         {             //create webrequest file             httpwebrequest filereq = (httpwebrequest)httpwebrequest.create("http://some-other-server.com/file.rar");               //create response request             httpwebresponse fileresp = (httpwebresponse)filereq.getresponse();               if (filereq.contentlength > 0)                 fileresp.contentlength = filereq.contentlength;              //get stream returned response             stream = fileresp.getresponsestream();              // prepare response client. resp client response             var resp = httpcontext.response;              //indicate type of data being sent             resp.contenttype = "application/octet-stream";              //name file              resp.addheader("content-disposition", "attachment; filename=\"" + "fle.rar" + "\"");             resp.addheader("content-length", (fileresp.contentlength).tostring());              int length;                         {                 // verify client connected.                 if (resp.isclientconnected)                 {                     // read data buffer.                     length = stream.read(buffer, 0, bytestoread);                      // , write out response's output stream                     resp.outputstream.write(buffer, 0, length);                      // flush data                     resp.flush();                      //clear buffer                     buffer = new byte[bytestoread];                 }                 else                 {                     // cancel download if client has disconnected                     length = -1;                 }             } while (length > 0); //repeat until no data read         }                 {             if (stream != null)             {                 //close input stream                 stream.close();             }         }     } 

when go page downloads problem if stopped download won't resume again.

i searched issue , figured out there header "accept-ranges" must defined in connection in order support resume.

so added header didn't work.

handling range requests little bit more complicated that. in general need handle range , if-range headers in request , serve proper 206 partial content responses content-range, date , etag or content-location headers.

the article range requests in asp.net mvc – rangefileresult describes in details how create asp.net mvc actionresult range request support.

in case have check if other side (the 1 use filereq) support range request. if yes can request needed part (and preferably cache somewhere locally), if not need entire file , seek proper locations (in situation defintely want have local caching scenario).


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -