HTTP Response of downloaded file corrupt

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bendman
    New Member
    • Mar 2012
    • 1

    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?

    Code:
        protected void OpenEmail()
        {
            try
            {
                if (Request.QueryString.Count > 0)
                {
                    string strID = Request.QueryString["VisitId"];
                    if (strID != null)
                    {
                        string fileName = generateMessageFile(strID);
                        if (!string.IsNullOrEmpty(fileName))
                        {
                            //remove backslash from file name
                            while ((fileName.IndexOf("/") == 0) || (fileName.IndexOf("\\") == 0))
                            {
                                fileName = fileName.Remove(0, 1);
                            }
                            fileName = fileName.Replace("..\\", "");
                            //':', ';', '?', '*', '\', '/', '>', '<', '|', '''', '"' : Result[i] := '_';  -- but '; not replaced in library
                            fileName = Regex.Replace(fileName, "[\\?=<>:;\\*\\|\"]", "");
                        }
    
                        string filePath = "c:\\temp";
                        FileInfo info = new FileInfo(Path.Combine(filePath, fileName));
    
                        if (File.Exists(filePath + fileName))
                        {
                            FileStream fileStream = new FileStream(filePath + fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                            try
                            {
                                const int CHUNK_SIZE = 1024 * 10;
                                long iFileLength = fileStream.Length;
    
                                BinaryReader binaryReader = new BinaryReader(fileStream);
                                try
                                {
                                    Response.Buffer = false;
                                    Response.Clear();
                                    Response.ClearContent();
                                    Response.ClearHeaders();
                                    Response.Charset = String.Empty;
                                    Encoding headerEncoding = Encoding.GetEncoding(1252);
                                    Response.HeaderEncoding = headerEncoding;
                                    Response.ContentType = "application/octet-stream";
                                    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                                    Response.AddHeader("Content-Length", info.Length.ToString());
    
                                    binaryReader.BaseStream.Seek(0, SeekOrigin.Begin);
                                    int iMaxCount = (int)Math.Ceiling((iFileLength + 0.0) / CHUNK_SIZE);
                                    for (int i = 0; i < iMaxCount && Response.IsClientConnected; i++)
                                    {
                                        Response.BinaryWrite(binaryReader.ReadBytes(CHUNK_SIZE));
                                        Response.Flush();
                                    }
                                    Response.End();
                                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                                }
                                finally
                                {
                                    binaryReader.Close();
                                }
                                fileStream.Close();
                            }
                            finally
                            {
                                fileStream.Dispose();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
Working...