2015-07-20 46 views
2

這裏我想下載鏈接中的空Excel文件按鈕單擊物理路徑中的事件。 我的代碼是如何下載物理路徑中的空Excel文件

HttpResponse httpResponse = HttpContext.Current.Response; 
     httpResponse.Clear(); 
     httpResponse.ClearContent(); 
     httpResponse.ClearHeaders();   

     httpResponse.AddHeader("content-disposition", "attachment;filename=" + txt1.Text + ".xls");    
     string strpath = ConfigurationManager.AppSettings["Downloads"].ToString() + txt1.Text;   
     httpResponse.ContentType = "application/vnd.ms-excel;"; 
     StringWriter sw = new StringWriter(); 
     HtmlTextWriter hw = new HtmlTextWriter(sw); 
     string style = @"<style> .textmode { mso-number-format:\@; } </style>"; 
     httpResponse.Write(style); 
     httpResponse.Output.Write(sw.ToString()); 

     httpResponse.Flush(); 
     httpResponse.Close(); 
     httpResponse.End(); 

我Web.Config中AddKey是

<add key="Downloads" value="C:\Users\MyName\Desktop\Downloads"/> 

在這裏,我怎麼給它添加鍵值

+0

「Down load」=>「下載」 – Fluff

+0

我想下載 – Exception

+1

你想t從HTTP URL下載文件(xls)並將其存儲在文件系統中? –

回答

0

也許這可以幫助你WebClient.DownloadFile

using (var client = new WebClient()) 
{ 
    client.DownloadFile(theUrl, 
     Path.Combine(ConfigurationManager.AppSettings["Downloads"].ToString(), "myXls.xls")); 
} 
+0

IAM使用HTTP響應 – Exception

+0

拋出'HttpResponse'被設計爲發送數據,它代表了你的服務器的客戶端請求 –

+0

確定...我在哪裏可以調用這個Web客戶端響應(),我的意思是在httpResponse.Output.Write(sw.ToString())之後; – Exception

相關問題