[英]Upload image from client machine to server using httpWebRequest Post
[英]Upload from server db and open file on client machine
如何組織從服務器數據庫下載文件並在客戶端計算機上打開文件?
我的代碼僅在服務器上打開頁面時有效:
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);
這在Web應用程序中是不可能的。
如果要執行此操作,請編寫Windows應用程序。
瀏覽器的安全限制不允許您從瀏覽器下載並直接運行exe。
您可以在響應中發送該exe,用戶可以在其中手動保存並運行它。
在當前設置下,無論請求來自何處,exe仍在編寫並在服務器上運行。
只是一點說明,使用WriteAllBytes
將字節數組寫入文件要容易WriteAllBytes
。 您不必擔心鎖和那樣處理對象。
File.WriteAllBytes(FileName, file);
感謝您的回答! 我這樣做是這樣的:
Response.ContentType = "APPLICATION/OCTET-STREAM";
Response.AddHeader("Content-Disposition", @"attachment;filename=\" + initFileName);
Response.BinaryWrite(file);
其中:file-它是二進制類型的參數。 initFileName-字符串類型的文件名。 只有3行)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.