I am developing a website where I am uploading document and stored in folder. Uploading document works fine but download code not works. I need to download file from folder.
protected void btnDownload_Click(object sender, EventArgs e)
{
lblresume.Text = "~/Student_Resume/" + fuResume.FileName.ToString();
if (lblresume.Text != string.Empty)
{
string filePath = lblresume.Text;
Response.ContentType = "doc/docx";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
}
}
try this
protected void btnDownload_Click(object sender, EventArgs e)
{
lblresume.Text = "~/Student_Resume/" + fuResume.FileName.ToString();
if (lblresume.Text != string.Empty)
{
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
string filePath = lblresume.Text;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition", "attachment;filename=Filename.extension");
byte[] data = req.DownloadData(Server.MapPath(filePath));
response.BinaryWrite(data);
response.End();
}
}
Filepath
in
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
Should be physical path; and not styart with: ~
.
Content-type should be "application/ms-word"
protected void btnDownload_Click(object sender, EventArgs e)
{
lblresume.Text = "~/Student_Resume/" + fuResume.FileName.ToString();
if (lblresume.Text != string.Empty)
{
string filePath = lblresume.Text;
Response.ContentType = "doc/docx";
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fuResume.FileName.ToString() + "\"");
Response.TransmitFile(Server.MapPath(filePath));
Response.End();
}
}
Try this code. This should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.