This is my project in this I am using Button link to download, I am clicking on it then it is giving me error
Click on the link button then my file is downloaded but it is giving error so
Please solve this error.
Error. Could not find file
SQL Code.
[ID] int identity (1,1), [FileName] VARCHAR (50), [FilePath] VARCHAR (50)
HTML
<asp:FileUpload ID="fileUpload1" runat="server" /><br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
<hr />
<asp:GridView ID="gvDetails" CssClass="Gridview" runat="server" AutoGenerateColumns="false" DataKeyNames="FilePath">
<HeaderStyle BackColor="#df5015" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="FileName" HeaderText="FileName" />
<asp:TemplateField HeaderText="FilePath">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
this.BindGridView();
}
}
private void BindGridView()
{
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using(SqlCommand cmd = new SqlCommand("SELECT * FROM UploadFile",con))
{
using(SqlDataAdapter sad = new SqlDataAdapter(cmd))
{
using(DataTable dt = new DataTable())
{
sad.Fill(dt);
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
}
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(fileUpload1.PostedFile.FileName);
fileUpload1.SaveAs(Server.MapPath("~/Files/" + filename));
//string query = "PS_UploadFile_Insert";
string constring = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO UploadFile (FileName,FilePath) VALUES (@FileName,@FilePath)", con))
{
cmd.Parameters.AddWithValue("@FileName", filename);
cmd.Parameters.AddWithValue("@FilePath", filename);
//cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
this.BindGridView();
}
}
}
protected void lnkDownload_Click(object sender, EventArgs e)
{
LinkButton button = sender as LinkButton;
GridViewRow gvrow = button.NamingContainer as GridViewRow;
string filePath = gvDetails.DataKeys[gvrow.RowIndex].Value.ToString();
Response.ContentType = "image/jpg";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
}