Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi Everyone,

I'm working on a messages module of a project in which registered users can send messages to each other. Everything works fine in case of uploading attachments and sending message but in INBOX module, there is a problem in downloading attachments. Below is the code which I'm using for uploading and downloading attachments.

Uploading and Sending Message:
C#
string s = txtids.Text;
string[] eids = s.Split(',');

if (FileUpload1.HasFile)
            {
                //SqlDataSource1.Insert();
                string fileName = FileUpload1.PostedFile.FileName;
                string fileType = FileUpload1.PostedFile.ContentType;
                int contentLength = Convert.ToInt32(FileUpload1.PostedFile.InputStream.Length);
                byte[] bytContent = new byte[contentLength];
                int status = FileUpload1.PostedFile.InputStream.Read(bytContent, 0, contentLength);
                
                foreach (string eid in eids)
                {
                        cmd = new SqlCommand("insert into messages(sender_ID, receiver_ID, m_subject, m_content, file_name, file_extension, file_content) values ('" + Session["uid"] + "','" + eid.ToString().Trim() + "','" + txtsub.Text.Trim() + "','" + txtbody.Text.Trim() + "', @FileName, @Extension, @Content)", con);
                        SqlParameter prmFileName = new SqlParameter("@FileName", SqlDbType.NVarChar, 1000);
                        prmFileName.Value = fileName;
                        cmd.Parameters.Add(prmFileName);
                        SqlParameter prmContentType = new SqlParameter("@Extension", SqlDbType.NVarChar, 300);
                        prmContentType.Value = fileType;
                        cmd.Parameters.Add(prmContentType);
                        SqlParameter prmbytContent = new SqlParameter("@Content", SqlDbType.VarBinary);
                        prmbytContent.Value = bytContent;
                        cmd.Parameters.Add(prmbytContent);
                        cmd.ExecuteNonQuery();
                }
                Response.Redirect("compose_message.aspx");
            }
            else
            {
                String str = "N/A";
                byte[] arr = System.Text.Encoding.ASCII.GetBytes(str); 
                foreach (string eid in eids)
                {
                    cmd = new SqlCommand("insert into messages(sender_ID, receiver_ID, m_subject, m_content, file_name, file_extension, file_content) values ('" + Session["uid"] + "','" + eid.ToString().Trim() + "','" + txtsub.Text.Trim() + "','" + txtbody.Text.Trim() + "',@FileName, @Extension, @Content)", con);
                    SqlParameter prmFileName = new SqlParameter("@FileName", SqlDbType.NVarChar, 1000);
                    prmFileName.Value = str;
                    cmd.Parameters.Add(prmFileName);
                    SqlParameter prmContentType = new SqlParameter("@Extension", SqlDbType.NVarChar, 300);
                    prmContentType.Value = str;
                    cmd.Parameters.Add(prmContentType);
                    SqlParameter prmbytContent = new SqlParameter("@Content", SqlDbType.VarBinary);
                    prmbytContent.Value = arr;
                    cmd.Parameters.Add(prmbytContent);
                    cmd.ExecuteNonQuery();
                }
            }

Source Code for Downloading Files:
C#
protected void LinkButton1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["E-PM"].ConnectionString);
    SqlCommand cmd = new SqlCommand("Select file_extension, file_content from messages where m_ID = '" + Session["mid"] + "'", conn);
    conn.Open();

    SqlDataReader Reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    Reader.Read();
    Response.Clear();

    Response.ContentType = Reader["file_extension"].ToString();
    byte[] reportFile = (byte[])Reader["file_content"];

    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.BinaryWrite(reportFile);
    Response.Flush();
    Response.End();
    conn.Close();
}

What I want is when user click on Link Button (Download), a file-save dialog box should open and ask user for saving location of file.

Any kind of help is very much appreciated.

Thank You
Posted
Updated 29-Dec-11 10:27am
v2

 
Share this answer
 
Comments
Sam_IDEA 29-Dec-11 16:18pm    
thank you for replying..
I got what the problem was. But can you help me in managing different file extensions at downloading time??
I forgot to add one attribute:
C#
Response.AddHeader("Content-Disposition", "attachment;filename=" + fname); 

So, nnow the whole code works absolutely fine.. :)
 
Share this answer
 
v2
Comments
Wendelius 29-Dec-11 17:05pm    
Pre tags added. Thanks for sharing the solution!

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