473,396 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

HTTP Response of downloaded file corrupt

Hi,

I generate at server side a *.MSG File (with inluded attachments). The code works. At server side the generated msg file is correct in filesystem. After Download via Browser the size of the file is nearby twice of the original file and corrupt. Has anyone an idea what's wrong here?

Expand|Select|Wrap|Line Numbers
  1.     protected void OpenEmail()
  2.     {
  3.         try
  4.         {
  5.             if (Request.QueryString.Count > 0)
  6.             {
  7.                 string strID = Request.QueryString["VisitId"];
  8.                 if (strID != null)
  9.                 {
  10.                     string fileName = generateMessageFile(strID);
  11.                     if (!string.IsNullOrEmpty(fileName))
  12.                     {
  13.                         //remove backslash from file name
  14.                         while ((fileName.IndexOf("/") == 0) || (fileName.IndexOf("\\") == 0))
  15.                         {
  16.                             fileName = fileName.Remove(0, 1);
  17.                         }
  18.                         fileName = fileName.Replace("..\\", "");
  19.                         //':', ';', '?', '*', '\', '/', '>', '<', '|', '''', '"' : Result[i] := '_';  -- but '; not replaced in library
  20.                         fileName = Regex.Replace(fileName, "[\\?=<>:;\\*\\|\"]", "");
  21.                     }
  22.  
  23.                     string filePath = "c:\\temp";
  24.                     FileInfo info = new FileInfo(Path.Combine(filePath, fileName));
  25.  
  26.                     if (File.Exists(filePath + fileName))
  27.                     {
  28.                         FileStream fileStream = new FileStream(filePath + fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
  29.                         try
  30.                         {
  31.                             const int CHUNK_SIZE = 1024 * 10;
  32.                             long iFileLength = fileStream.Length;
  33.  
  34.                             BinaryReader binaryReader = new BinaryReader(fileStream);
  35.                             try
  36.                             {
  37.                                 Response.Buffer = false;
  38.                                 Response.Clear();
  39.                                 Response.ClearContent();
  40.                                 Response.ClearHeaders();
  41.                                 Response.Charset = String.Empty;
  42.                                 Encoding headerEncoding = Encoding.GetEncoding(1252);
  43.                                 Response.HeaderEncoding = headerEncoding;
  44.                                 Response.ContentType = "application/octet-stream";
  45.                                 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
  46.                                 Response.AddHeader("Content-Length", info.Length.ToString());
  47.  
  48.                                 binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);
  49.                                 int iMaxCount = (int)Math.Ceiling((iFileLength + 0.0) / CHUNK_SIZE);
  50.                                 for (int i = 0; i < iMaxCount && Response.IsClientConnected; i++)
  51.                                 {
  52.                                     Response.BinaryWrite(binaryReader.ReadBytes(CHUNK_SIZE));
  53.                                     Response.Flush();
  54.                                 }
  55.                                 Response.End();
  56.                                 HttpContext.Current.ApplicationInstance.CompleteRequest();
  57.                             }
  58.                             finally
  59.                             {
  60.                                 binaryReader.Close();
  61.                             }
  62.                             fileStream.Close();
  63.                         }
  64.                         finally
  65.                         {
  66.                             fileStream.Dispose();
  67.                         }
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.         catch (Exception ex)
  73.         {
  74.         }
  75.     }
  76.  
  77.  
Mar 2 '12 #1
0 1390

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: SS | last post by:
We built an MSI file to deploy a .net app to the workstation. We created an HTML page that has a link to the MSI file. On a very sporadic basis, when the MSI is run we get the following message:...
1
by: Christian Knoblauch | last post by:
Hello, i have a question regarding HTTP request/response using ASP.NET. PROBLEM: My client-application is behind a firewall, so that the server must be reached using HTTP. A started job on...
2
by: | last post by:
Hello All, I am writing a web application that reads a bitmap from a file and outputing it to a HTTP response stream to return the image to the requesting client. The image file is a regular...
0
by: SimonDev | last post by:
Hi I've got an unusual problem I'm hoping someone could advise me on, regarding the formatting of the body of an HTTP response from a web service. We are using HTTP POST rather than SOAP for...
0
by: makinha | last post by:
Hello All, I can use WebClient.DownloadFile Method to download target file from the website, like WebClient.DownloadFile("http://www.abc.com/a.pdf", "D:\a.pdf") but how can I verify the...
2
by: boole | last post by:
Hi there, when my ASP page receives the client request, I want to gather the request data (form), promptly end the response to the client (successful) and continue doing what I need to do with the...
0
by: burkenstein | last post by:
This looks like the important part: 2010-04-26 16:14:40.049848 wsdl: HTTP ERROR: Unsupported HTTP response status 403 Forbidden (soapclient->response has contents of the response) 2010-04-26...
4
by: Tinus | last post by:
Hi I have a php code that uploads a file to mysql db then I detrmine the id of the last inserted file and download the file .e user is asked if he wants to save the file and then asked for te...
2
by: neovantage | last post by:
Hi geeks, We made a Java Servlet which takes 3 parameters and upload files from source path to remote path. The three parameters are password, source path and destination path. When we run this...
5
by: shinsengumi | last post by:
Hi, What command/technique is needed in a C program to receive image bytes through socket programming from an HTTP response? I want to develop a program that requests for an image from a server,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.