How do I download big file of applications
-
I can download small size of application but could not download big size of application. It shows out of memory. Below is my code:
string Files = ListBox1.SelectedValue;
byte[] fileBytes = System.IO.File.ReadAllBytes(Files);WebClient User = new WebClient(); byte\[\] FileBuffer = User.DownloadData(Files); if (FileBuffer != null) { System.Web.HttpContext context = System.Web.HttpContext.Current; context.Response.Clear(); context.Response.ClearHeaders(); context.Response.ClearContent(); context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("content-length", FileBuffer.Length.ToString()); context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(Files)); context.Response.BinaryWrite(FileBuffer); context.ApplicationInstance.CompleteRequest();
Could pls anyone help...
-
I can download small size of application but could not download big size of application. It shows out of memory. Below is my code:
string Files = ListBox1.SelectedValue;
byte[] fileBytes = System.IO.File.ReadAllBytes(Files);WebClient User = new WebClient(); byte\[\] FileBuffer = User.DownloadData(Files); if (FileBuffer != null) { System.Web.HttpContext context = System.Web.HttpContext.Current; context.Response.Clear(); context.Response.ClearHeaders(); context.Response.ClearContent(); context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("content-length", FileBuffer.Length.ToString()); context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(Files)); context.Response.BinaryWrite(FileBuffer); context.ApplicationInstance.CompleteRequest();
Could pls anyone help...
-
Member 12016106 wrote:
byte[] fileBytes = System.IO.File.ReadAllBytes(Files);
Do not assume that there is enough memory to hold the entire file.
So how should declare it to download large size file??
-
So how should declare it to download large size file??
Simply put, computers don't have unlimited memory. Determine a reasonable buffer size and read the file in chunks of that size, processing each one in turn. If your data has any kind of structure (either fixed size blocks, or flexible e.g. XML) you should make use of that to work out how to read and process it in smaller pieces. I can't tell from your sample code what you're trying to do, but it appears you're accessing both local and remote files, either of which could be much larger than your available memory. You need to think through what processing you need to do, and how to achieve it within the limits.
Days spent at sea are not deducted from one's alloted span - Phoenician proverb
-
I can download small size of application but could not download big size of application. It shows out of memory. Below is my code:
string Files = ListBox1.SelectedValue;
byte[] fileBytes = System.IO.File.ReadAllBytes(Files);WebClient User = new WebClient(); byte\[\] FileBuffer = User.DownloadData(Files); if (FileBuffer != null) { System.Web.HttpContext context = System.Web.HttpContext.Current; context.Response.Clear(); context.Response.ClearHeaders(); context.Response.ClearContent(); context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("content-length", FileBuffer.Length.ToString()); context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(Files)); context.Response.BinaryWrite(FileBuffer); context.ApplicationInstance.CompleteRequest();
Could pls anyone help...
You have several options:
- Use the
Response.TransmitFile
method[^]; - Use the
Response.WriteFile
method[^]; - Read the file one block at a time and write each block to the response before reading the next block;
TransmitFile
is the preferred method:string physicalPath = ListBox1.SelectedValue;
if (!string.IsNullOrWhiteSpace(physicalPath))
{
FileInfo fileToSend = new FileInfo(fileToSend);
if (fileToSend.Exists)
{
System.Web.HttpContext context = System.Web.HttpContext.Current;
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("content-length", fileToSend.Length.ToString());
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlPathEncode(fileToSend.Name));
context.Response.TransmitFile(fileToSend.FullName);
context.Response.Flush();
context.ApplicationInstance.CompleteRequest();
}
}WriteFile
will work in the same way, but can have problems with large files[^]. Reading the file in small blocks requires more code, and won't perform as well asTransmitFile
:// (Same code as before, up to and including the "Content-Disposition" header)
int bytesRead;
byte[] buffer = new byte[10000];
using (Stream fileStream = fileToSend.OpenRead())
{
while (context.Response.IsClientConnected && (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
context.Response.OutputStream.Write(buffer, 0, bytesRead);
context.Response.Flush();
}
}context.ApplicationInstance.CompleteRequest();
- Use the