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:
string s = txtids.Text;
string[] eids = s.Split(',');
if (FileUpload1.HasFile)
{
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:
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