簡體   English   中英

從asp.net中的文件夾下載文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM