如何组织从服务器数据库下载的文件并在客户端计算机上打开它?
我的代码仅在服务器上打开页面时起作用:
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 :(得分:0)
这在网络应用程序中是不可能的 如果要执行此操作,请编写Windows应用程序。
浏览器的安全限制不允许您从浏览器下载并直接运行exe 您可以在响应中发送exe,用户可以保存并手动运行它。
在您当前的设置中,无论请求是否来自,exe仍在服务器上编写并运行。
只是旁注,使用WriteAllBytes
将字节数组写入文件会更容易。
您不必担心锁定和处理对象。
File.WriteAllBytes(FileName, file);
答案 1 :(得分:0)
谢谢你的回答!我是这样做的:
Response.ContentType = "APPLICATION/OCTET-STREAM";
Response.AddHeader("Content-Disposition", @"attachment;filename=\" + initFileName);
Response.BinaryWrite(file);
其中:file - 它是二进制类型的参数。 initFileName - 字符串类型文件名。 只有3行)。