2012-11-07 33 views
0

如何組織從服務器數據庫下載文件並在客戶機上打開它?從服務器數據庫上載並打開客戶機上的文件

我的代碼工作,只有當打開頁面時在服務器上:

 OracleCommand oracleCom = new OracleCommand(); 
     oracleCom.Connection = oraConnect; 
     oracleCom.CommandText = "Select m_content, f_extension From " + Session["tableNameIns"] + 
              " where i_id = " + rowData; 
     OracleDataAdapter adapter = new OracleDataAdapter(); 
     DataTable tableD = new DataTable(); 
     tableD.Locale = System.Globalization.CultureInfo.InvariantCulture; 
     adapter.SelectCommand = oracleCom; 
     adapter.Fill(tableD); 

     string FileName = Path.GetTempPath(); 
     FileName += "tempfile" + tableD.Rows[0][1]; 

     byte[] file = new byte[0]; 
     file = (byte[])tableD.Rows[0][0]; 
     FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write); 
     fs.Write(file, 0, file.GetUpperBound(0) + 1); 
     fs.Close(); 
     System.Diagnostics.Process.Start(FileName); 

回答

0

這僅僅是不可能在一個Web應用程序。
如果你想這樣做,寫一個Windows應用程序。

對瀏覽器的安全限制不允許您從瀏覽器下載並直接運行exe。
您可以在響應中發送exe,其中用戶手動保存並運行它。

在您當前的設置中,無論請求來自何方,該exe文件仍在寫入並在服務器上運行。

只是一個側面說明,它更容易使用WriteAllBytes寫入字節數組到文件。 您不必擔心鎖定和處理對象的方式。

File.WriteAllBytes(FileName, file); 
0

感謝您的回答!我這樣做:

Response.ContentType = "APPLICATION/OCTET-STREAM"; 
Response.AddHeader("Content-Disposition", @"attachment;filename=\" + initFileName); 
Response.BinaryWrite(file); 

其中:文件 - 它是二進制類型的參數。 initFileName - 字符串類型的文件名。 只有3行)。

相關問題