Click here to Skip to main content
15,906,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Sir,

I have some problems when i am implemented code in the download attachment. The Problem is if i am attached file in 150 kb then i am download this file he gives me
60 kb only. The properly not download any attachment file and if any data saved on it then you have download this file not given any data or blanck data give me this document. So could you please suggest me.

Thanks in Advanced !!
C#
protected void DownloadAttachmentCompany()
   {
       //File Path and File Name
       string obj_attachmentFilename;

       cls_bl_Company obj = new cls_bl_Company();

       mstr_ConnectionString = Session[SessionController.SESSION_CONSTRING].ToString();
       mint_LanguageId = Convert.ToInt32(Session[SessionController.SESSION_LANGID].ToString());

       //  DataSet objary_AttachmentText = new DataSet();
       mobj_company = new clsCompany(mstr_ConnectionString);

       int int_ClientId = Convert.ToInt32(Session[SessionController.SESSION_CLIENTID]);

       int int_CompanyId = Convert.ToInt32(Session[SessionController.SESSION_LOADEDCOMPANY]);

       int AttachmentId = Convert.ToInt32(aspLstBox.SelectedValue);

       obj_attachmentFilename = mobj_company.getattachmentfilename(AttachmentId , int_CompanyId, int_ClientId);

       string filePath = Server.MapPath("~/Attachments/");

       //string _DownloadableProductFileName = aspupld_attachments.FileName;
       string _DownloadableProductFileName = obj_attachmentFilename;

       string strFileexits = filePath + _DownloadableProductFileName;

       System.IO.FileInfo FileName = new System.IO.FileInfo(filePath + _DownloadableProductFileName);


       if (File.Exists(strFileexits))
       {

           FileStream myFile = new FileStream(filePath + "//" + _DownloadableProductFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite,FileShare.ReadWrite);

           //Reads file as binary values
           BinaryReader _BinaryReader = new BinaryReader(myFile);

           long startBytes = 0;
           string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
           string _EncodedData = HttpUtility.UrlEncode
               (_DownloadableProductFileName, Encoding.UTF8) + lastUpdateTiemStamp;

           //Clear the content of the response
           Response.Clear();
           Response.Buffer = false;
           Response.AddHeader("Accept-Ranges", "bytes");
           Response.AppendHeader("ETag", "\"" + _EncodedData + "\"");
           Response.AppendHeader("Last-Modified", lastUpdateTiemStamp);

           //Set the ContentType
           Response.ContentType = "application/octet-stream";

           //Add the file name and attachment,
           //which will force the open/cancel/save dialog to show, to the header
           Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName.Name);

           //Add the file size into the response header
           Response.AddHeader("Content-Length", (FileName.Length - startBytes).ToString());
           Response.AddHeader("Connection", "Keep-Alive");

           //Set the Content Encoding type
           Response.ContentEncoding = Encoding.UTF8;

           //Send data
           _BinaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);
       }
       else
       {
           MessageBox.Show("File does not Exits");
       }

       this.EnableModelDialog(true);

       //((ModalPopupExtender)(this.Parent.FindControl("extender_Attachment"))).Show();
   }


   /// <summary>
   /// Load Attachment Header Assigenment functionality.
   /// </summary>
   protected void LoadAttachmentHeaderAssigenment()
   {
       cls_bl_Assignment[] objary_AttachmentText = new cls_bl_Assignment[25];
       clsAssignment mobj_Assigenment = new clsAssignment(mstr_ConnectionString);

       int int_ClientId = Convert.ToInt32(Session[SessionController.SESSION_CLIENTID]);

       int int_AssigenmentId = Convert.ToInt32(Session[SessionController.SESSION_LOADEDASSIGNMENT]);

       objary_AttachmentText = mobj_Assigenment.LoadAttachmentHeaderAssigenment(int_AssigenmentId, int_ClientId);

       int intGotIndex = 0;
       if (Page.IsPostBack)
       {
           aspLstBox.Items.Clear();

           if (null != objary_AttachmentText)
           {
               for (int forcount = 0; forcount < objary_AttachmentText.Length; forcount++)
               {
                   intGotIndex = 0;
                   for (int intforcount = 0; intforcount < aspLstBox.Items.Count; intforcount++)
                   {
                       if (objary_AttachmentText[forcount].Text == aspLstBox.Items[intforcount].Text)
                       {
                           intGotIndex = 1;
                       }
                   }
                   if (0 == intGotIndex)
                   {
                       aspLstBox.Items.Add(new ListItem(objary_AttachmentText[forcount].Text.ToString(), objary_AttachmentText[forcount].AttachmentId.ToString()));
                   }
               }
           }
       }
   }

in this code you have added attachment id from list box control and all other id from session control so please give me some improvements in this code.

Thanks in Advanced !!
Posted
Updated 24-Jul-12 20:52pm
v3
Comments
OriginalGriff 25-Jul-12 2:03am    
Without seeing your code fragment for the download? Not a lot we can suggest!
Use the "Improve question" widget to edit your question and provide better information.
JakirBB 25-Jul-12 2:03am    
Are you an American? I wonder!!
Aditya Mangipudi 2-Aug-12 9:54am    
FileStream myFile = new FileStream(filePath + "//" + _DownloadableProductFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite,FileShare.ReadWrite);

Check this part again. Why are you adding "//" again. (filePath + "//" + _DownloadableProductFileName)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900