1 HttpResponse response = HttpContext.Current.Response; 2 string js = "<script language=javascript>window.open('{0}');</script>"; 3 js = string.Format(js, url); 4 response.Write(js); |
但是有個問題了,就是會被廣告攔截軟體直接攔截掉,另我非常的頭痛,於是尋找更好的解決方法.看了用Response.BinaryWrite寫檔案流一文之後覺得確實可以如此,修改程式碼如下:
1/**//**//**//// <summary> 2 /**//// 下載檔案 3 /**//// </summary> 4 /**//// <param name="filename">檔案實體地址</param> 5 6protected void DownloadFile(string filename) 7 ...{ 8 string saveFileName = "test.xls"; 9 int intStart = filename.LastIndexOf("\")+1; 10 saveFileName = filename.Substring(intStart,filename.Length-intStart); 11 FileStream MyFileStream; 12 long FileSize; 13 14 MyFileStream = new FileStream(filename,FileMode.Open); 15 FileSize = MyFileStream.Length; 16 17 byte[] Buffer = new byte[(int)FileSize]; 18 MyFileStream.Read(Buffer, 0, (int)FileSize); 19 MyFileStream.Close(); 20 21 Response.AddHeader("Content-Disposition", "attachment;filename="+saveFileName); 22 Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 23 Response.ContentType = "application/vnd.ms-excel"; 24 25 Response.BinaryWrite(Buffer); 26 Response.Flush(); 27 Response.Close(); 28 Response.End(); 29 30 } |
但是有個嚴重的問題,就是檔案格式。這樣只是將流輸出,且無法正確識別格式。還好,能人層出不窮, 柚子Nan 提出了能否不考慮檔案的型別,直接把檔案顯示到瀏覽器(Response) 的想法正好切中我的要害所在,於是急忙研究了柚子Nan的想法,修改出最後程式碼:
1 /**//**//**//// <summary> 2 /**//// 下載檔案 3 /**//// </summary> 4 /**//// <param name="filename">檔案實體地址</param> 5 protected void DownloadFile(string filename) 6 ...{ 7 string saveFileName = "test.xls"; 8 int intStart = filename.LastIndexOf("\")+1; 9 saveFileName = filename.Substring(intStart,filename.Length-intStart); 10 11 Response.Clear(); 12 Response.Charset = "utf-8"; 13 Response.Buffer= true; 14 this.EnableViewState = false; 15 Response.ContentEncoding = System.Text.Encoding.UTF8; 16 17 Response.AppendHeader("Content-Disposition","attachment;filename=" + saveFileName); 18 Response.WriteFile(filename); 19 Response.Flush(); 20 Response.Close(); 21 22 Response.End(); 23 } |
使用昨天Asp.net直接儲存檔案到客戶端 中的方法,經過我的反覆測試,各式文件都執行完全正常。於是昨晚修改了現有程式碼,修改了下載方法,以解決一直困擾自己的視窗攔截問題。
早上本來還沾沾自喜,這下再也不用老跟客戶解釋為什麼視窗會沒掉了。可惜啊,人算不如天算。
早上客戶就反映下載的檔案全是亂碼。立馬在本機進行測試,沒問題。再同事的機器上試驗,同樣沒問題。
那應該是客戶端的問題才是。只好讓客戶NetMeeting演示一下她的操作過程。下載-〉儲存-〉開啟。這麼簡單的流程,不會做錯吧?
正在鬱悶之際,突然腦光一閃,終於發現不一樣的地方,立馬試驗,果然如此!
到底有什麼區別呢?請看操作圖:
客人操作圖
我的操作圖
各位應該看出不同之處了吧?還看不出來?
這件事情的罪魁禍首就是:
解決方法:使用lovecherry 的如何從登錄檔讀取檔案的ContentType 一文的方法
修正程式碼: 1 /**//// <summary> 2 /// 下載檔案 3 /// </summary> 4 /// <param name="filename">檔案實體地址</param> 5 protected void DownloadFile(string filename) 6 { 7 8 string saveFileName = "test.xls"; 9 int intStart = filename.LastIndexOf("\\")+1; 10 saveFileName = filename.Substring(intStart,filename.Length-intStart); 11 12 System.IO.FileInfo fi=new System.IO.FileInfo(filename); 13 string fileextname=fi.Extension; 14 string DEFAULT_CONTENT_TYPE = "application/unknown"; 15 RegistryKey regkey,fileextkey; 16 string filecontenttype; 17 try 18 { 19 regkey=Registry.ClassesRoot; 20 fileextkey=regkey.OpenSubKey(fileextname); 21 filecontenttype=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString(); 22 } 23 catch 24 { 25 filecontenttype=DEFAULT_CONTENT_TYPE; 26 } 27 28 29 Response.Clear(); 30 Response.Charset = "utf-8"; 31 Response.Buffer= true; 32 this.EnableViewState = false; 33 Response.ContentEncoding = System.Text.Encoding.UTF8; 34 35 Response.AppendHeader("Content-Disposition","attachment;filename=" + saveFileName); 36 Response.ContentType=filecontenttype; 37 38 Response.WriteFile(filename); 39 Response.Flush(); 40 Response.Close(); 41 42 Response.End(); 43 } |
最後得出結論:要實現柚子Nan提出的能否不考慮檔案的型別,直接把檔案顯示到瀏覽器(Response),有一種方法,讓客戶端都不要隱藏已知的副檔名,但是這種方法是無法適應大部分電腦使用者的(一般只有比較熟悉電腦的人才會這樣做吧?)
bbs 看中的方法,還沒有試用,不知道有沒有作用.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load '在此處放置初始化頁的使用者程式碼 ' 定義是否是 SQL Server 資料庫,這裡為False Dim blnIsSQLServer As System.Boolean = False Dim strSQL As String Dim objDataset As New DataSet() Dim objConn As Object Dim strCnn As String If blnIsSQLServer Then strCnn = "User ID=sa;Initial Catalog=Northwind;Data Source=.\NetSDK;" objConn = New System.Data.SqlClient.SqlConnection(strCnn) objConn.Open() Dim objAdapter As New System.Data.SqlClient.SqlDataAdapter() strSQL = "Select * from customers where country='USA'" objAdapter.SelectCommand = New System.Data.SqlClient.SqlCommand(strSQL, objConn) objAdapter.Fill(objDataset) Else strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Test.mdb") objConn = New System.Data.OleDb.OleDbConnection(strCnn) objConn.Open() Dim objAdapter As New System.Data.OleDb.OleDbDataAdapter() strSQL = "Select Top 10 Title From Document" objAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand(strSQL, objConn) objAdapter.Fill(objDataset) End If Dim oView As New DataView(objDataset.Tables(0)) DataGrid1.DataSource = oView DataGrid1.DataBind() objConn.Close() objConn.Dispose() objConn = Nothing If Request.QueryString("bExcel") = "1" Then Response.ContentType = "application/vnd.ms-excel" ' 從Content-Type header中去除charset設定 Response.Charset = "" ' 關閉 ViewState Me.EnableViewState = False Dim tw As New System.IO.StringWriter() Dim hw As New System.Web.UI.HtmlTextWriter(tw) ' 獲取control的HTML DataGrid1.RenderControl(hw) ' 把HTML寫回瀏覽器 Response.Write(tw.ToString()) Response.End() End If End Sub |