从ASP.NET文件夹中下载文件

3

我正在开发一个网站,在其中上传文档并存储在文件夹中。上传文件正常工作,但下载代码不起作用。我需要从文件夹中下载文件。

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();         
         }    
}

尝试完全限定您文件的路径。 - Sargis Koshkaryan
"~/Student_Resume/" + fuResume.FileName.ToString 是一个完全限定路径吗?" - user3256868
路径应该是物理的。 - Sargis Koshkaryan
3个回答

8

试试这个

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();                   
     }    
}

byte[] data = req.DownloadData(filePath); 在这行代码中,我遇到了“找不到路径的一部分”的错误。 - user3256868
这行代码从文件上传工具中获取文件名,而不是从lblresume中获取。byte[] data = req.DownloadData(filePath); - user3256868
尝试使用Server.MapPath(filePath)。 - Rashad Valliyengal
先生,找不到路径的一部分:“C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0~\Student_Resume\”。这是一个错误。 - user3256868
学生简历文件夹的位置在哪里?它是否在根目录下?如果是,则可以使用字符串路径= Server.MapPath(“〜/ Student_Resume /”)+ @“\ resume.docx”; 并在此处使用byte [] data = req.DownloadData(path); - Rashad Valliyengal

1

Filepath in

Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\""); 

应该是物理路径,而不是以~开头。
Content-type 应该是"application/ms-word"

“Filepath” 应该是没有 “~” 的文件名和扩展名。这是在下载文件对话框中默认显示的文件名。 - rt2800
如何从文件夹访问文件名进行下载。此处它从fileupload获取文件名而非文件夹。 - user3256868
是的先生,但这行代码从文件上传工具中获取文件名而不是lblresume。Response.TransmitFile(Server.MapPath(filePath)); - user3256868
使用 Response.AddHeader("Content-Disposition", "attachment;filename=\"" + Path.GetFileName(Server.MapPath(filePath)) + "\""); 将 filePath 转换为 filename+ext。 - rt2800

0
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();         
     }    
}

试试这段代码,应该可以正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接