[英]Download file from folder in asp.net
我正在开发一个网站,我上传文件并存储在文件夹中。 上传文档工作正常,但下载代码不起作用。 我需要从文件夹下载文件。
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();
}
}
试试这个
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
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
应该是物理路径; 而不是styart与: ~
。
内容类型应为"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();
}
}
试试这个代码。 这应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.