Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 4.78 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Download function failing with big file sizes
  2. protected void downloadFunction(string fileName)
  3. {
  4.     string filePath = @"D:SoftwareFiles";
  5.     LogMessageToFile("Download started " + filePath + fileName);
  6.     byte[] array = File.ReadAllBytes(filePath + fileName);
  7.  
  8.  
  9.     Response.Clear();
  10.     Response.ContentType = "application/x-newton-compatible-pkg";
  11.     Response.AppendHeader("Content-Disposition",
  12.                           "attachment;filename=" + fileName);
  13.  
  14.     Response.BinaryWrite(array);
  15.     Response.End();
  16. }
  17.        
  18. using System;
  19. using System.IO;
  20. using System.Web;
  21.  
  22. public class Download
  23. {
  24.  
  25.  
  26. public static void SmallFile(string filename, string filepath, string contentType)
  27. {
  28. try
  29. {
  30. FileStream MyFileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
  31. long FileSize;
  32. FileSize = MyFileStream.Length;
  33. byte[] Buffer = new byte[(int)FileSize];
  34. MyFileStream.Read(Buffer, 0, (int)MyFileStream.Length);
  35. MyFileStream.Close();
  36. HttpContext.Current.Response.ContentType = contentType;
  37. HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
  38. HttpContext.Current.Response.BinaryWrite(Buffer);
  39.  
  40. }
  41. catch
  42. {
  43. HttpContext.Current.Response.ContentType = "text/html";
  44. HttpContext.Current.Response.Write("Downloading Error!");
  45. }
  46. HttpContext.Current.Response.End();
  47. }
  48. public static void LargeFile(string filename, string filepath, string contentType)
  49. {
  50. Stream iStream = null;
  51. // Buffer to read 10K bytes in chunk
  52. //byte[] buffer = new Byte[10000];
  53. // Buffer to read 1024K bytes in chunk
  54.  
  55.  
  56. byte[] buffer = new Byte[1048576];
  57.  
  58. // Length of the file:
  59.  
  60.  
  61. int length;
  62. // Total bytes to read:
  63.  
  64.  
  65. long dataToRead;
  66.  
  67. try
  68. {
  69. // Open the file.
  70.  
  71.  
  72. iStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
  73. // Total bytes to read:
  74.  
  75.  
  76. dataToRead = iStream.Length;
  77. HttpContext.Current.Response.ContentType = contentType;
  78. HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
  79. // Read the bytes.
  80.  
  81.  
  82. while (dataToRead > 0)
  83. {
  84. // Verify that the client is connected.
  85.  
  86.  
  87. if (HttpContext.Current.Response.IsClientConnected)
  88. {
  89. // Read the data in buffer.
  90.  
  91.  
  92. length = iStream.Read(buffer, 0, 10000);
  93.  
  94. // Write the data to the current output stream.
  95.  
  96.  
  97. HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
  98.  
  99. // Flush the data to the HTML output.
  100.  
  101.  
  102. HttpContext.Current.Response.Flush();
  103.  
  104. buffer = new Byte[10000];
  105. dataToRead = dataToRead - length;
  106. }
  107. else
  108. {
  109. //prevent infinite loop if user disconnects
  110.  
  111.  
  112. dataToRead = -1;
  113. }
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. // Trap the error, if any.
  119. //HttpContext.Current.Response.Write("Error : " + ex.Message);
  120.  
  121.  
  122. HttpContext.Current.Response.ContentType = "text/html";
  123. HttpContext.Current.Response.Write("Error : file not found");
  124.  
  125. }
  126. finally
  127. {
  128. if (iStream != null)
  129. {
  130. //Close the file.
  131.  
  132.  
  133. iStream.Close();
  134. }
  135. HttpContext.Current.Response.End();
  136. HttpContext.Current.Response.Close();
  137.  
  138. }
  139.  
  140. }
  141. public static void ResumableFile(string filename, string fullpath, string contentType)
  142. {
  143. try
  144. {
  145. FileStream myFile = new FileStream(fullpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  146. BinaryReader br = new BinaryReader(myFile);
  147. try
  148. {
  149. HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes");
  150. HttpContext.Current.Response.Buffer = false;
  151. long fileLength = myFile.Length;
  152. long startBytes = 0;
  153.  
  154. //int pack = 10240; //10K bytes
  155.  
  156.  
  157. int pack = 1048576; //1024K bytes
  158.  
  159.  
  160. if (HttpContext.Current.Request.Headers["Range"] != null)
  161. {
  162. HttpContext.Current.Response.StatusCode = 206;
  163. string[] range = HttpContext.Current.Request.Headers["Range"].Split(new char[] { '=', '-' });
  164. startBytes = Convert.ToInt64(range[1]);
  165. }
  166. HttpContext.Current.Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
  167. if (startBytes != 0)
  168. {
  169. HttpContext.Current.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
  170. }
  171. HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive");
  172. HttpContext.Current.Response.ContentType = contentType;
  173. HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
  174.  
  175. br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
  176. int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;
  177.  
  178. for (int i = 0; i < maxCount; i++)
  179. {
  180. if (HttpContext.Current.Response.IsClientConnected)
  181. {
  182. HttpContext.Current.Response.BinaryWrite(br.ReadBytes(pack));
  183. }
  184. else
  185. {
  186. i = maxCount;
  187. }
  188. }
  189. }
  190. catch
  191. {
  192. HttpContext.Current.Response.ContentType = "text/html";
  193. HttpContext.Current.Response.Write("Error : file not found");
  194. }
  195. finally
  196. {
  197. br.Close();
  198. myFile.Close();
  199. }
  200. }
  201. catch
  202. {
  203. HttpContext.Current.Response.ContentType = "text/html";
  204. HttpContext.Current.Response.Write("Error : file not found");
  205. }
  206. }
  207.  
  208. }