Hi @Sunny ,
How did you get your filename?
If the download is not successful, it should be that you have not obtained the correct filename.
You can refer to the example below. upload.aspx
<asp:TextBox ID="txtId" runat="server"></asp:TextBox>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />```
protected void Upload(object sender, EventArgs e)
{
string folderPath = Server.MapPath("~/Attachments/");
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string filename = Guid.NewGuid().ToString() + extension;
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
FileUpload1.SaveAs(folderPath + filename);
int Id = Convert.ToInt32(txtId.Text);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "insert into tblFiles(Id,Name, ContentType, Data) values (@Id ,@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", Id);
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Response.Redirect("track.aspx");
}
}
track.aspx
<asp:Label ID="Label1" runat="server" Text="Id:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="DownloadFile" Text="DownloadFile" runat="server" OnClick="DownloadFile_Click" />
protected void DownloadFile_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBox1.Text);
byte[] bytes;
string fileName, contentType;
string constr = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Name, Data, ContentType from tblFiles where Id=@Id";
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["Data"];
contentType = sdr["ContentType"].ToString();
fileName = sdr["Name"].ToString();
}
con.Close();
}
}
string filePath = Server.MapPath(string.Format("~/Attachments/{0}", fileName));
byte[] content = File.ReadAllBytes(filePath);
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(content);
Response.Flush();
Response.End();
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.