2014-11-05 78 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."); 
      } 
相关问题