2014-11-05 76 views
0
string filePath1 = (sender as LinkButton).CommandArgument; 
string filepath = ("D:\\RetailAgreement\\" + filePath1); 
FileInfo myfile = new FileInfo(filepath); 
if (filePath1 != "") 
{ 
    Response.ClearContent(); 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name); 
    Response.AddHeader("Content-Length", myfile.Length.ToString()); 
    Response.ContentType = ReturnExtension(myfile.Extension.ToLower()); 
    Response.TransmitFile(myfile.FullName); 
    Response.End(); 

} 

我試過這樣但它不工作, 我不知道我在哪裏出錯了。我正在使用C#3.0如何使用c下載服務器文件到客戶端系統#

+0

移動文件你的webapp下,並用'Server.MapPath'方法來獲得真實路徑(要下載這些文件)。 – adatapost 2014-11-05 04:43:32

+0

你是怎麼調用這段代碼的?你給我們提供了一個javascritp錯誤,但沒有javascript。這是一個AJAX調用嗎?如果是這樣,你錯誤地認爲你可以下載像這樣的文件。頁面標題已經設置,AJAX不會這樣做,並期望有一個非常不同的響應。 – 2014-11-05 04:43:34

+0

這不是一項艱鉅的任務。你不需要太多的迴應。看看http://rachelappel.com/upload-and-download-files-using-asp.net-mvc – 2014-11-05 08:19:26

回答

0

使用此代碼。

string path = Server.MapPath("~/DownloadedExcelFilesOp4/myfile.xlsx"); 
      System.IO.FileInfo file = new System.IO.FileInfo(path); 
      string Outgoingfile = "myfile.xlsx"; 
      if (file.Exists) 
      { 
       Response.Clear(); 
       Response.ClearContent(); 
       Response.ClearHeaders(); 
       Response.AddHeader("Content-Disposition", "attachment; filename=" + Outgoingfile); 
       Response.AddHeader("Content-Length", file.Length.ToString()); 
       Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
       Response.WriteFile(file.FullName); 
       Response.Flush(); 
       Response.Close(); 

      } 
      else 
      { 
       Response.Write("This file does not exist."); 
      } 
+0

你不需要'Server.MapPath'作爲絕對文件路徑... – leppie 2014-11-05 05:02:50

0
File.Copy("D:\\RetailAgreement\\",Server.Mapth("YourAplicationPath\\MyFiles"), true); 
//Copy Files to Your Application Path 
string path = Server.MapPath("~\MyFiles" + filePath1"); 
      System.IO.FileInfo file = new System.IO.FileInfo(path); 
      string Outgoingfile = "myfile.xlsx"; 
      if (file.Exists) 
      { 
       Response.Clear(); 
       Response.ClearContent(); 
       Response.ClearHeaders(); 
       Response.AddHeader("Content-Disposition", "attachment; filename=" + Outgoingfile); 
       Response.AddHeader("Content-Length", file.Length.ToString()); 
       Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
       Response.WriteFile(file.FullName); 
       Response.Flush(); 
       Response.Close(); 

      } 
      else 
      { 
       Response.Write("This file does not exist."); 
      } 
相關問題