C# 从服务器数据库上载并在客户端计算机上打开文件
如何组织从服务器数据库下载的文件并在客户机上打开它 “我的代码”仅在服务器上打开页面时有效:C# 从服务器数据库上载并在客户端计算机上打开文件,c#,asp.net,web-applications,C#,Asp.net,Web Applications,如何组织从服务器数据库下载的文件并在客户机上打开它 “我的代码”仅在服务器上打开页面时有效: OracleCommand oracleCom = new OracleCommand(); oracleCom.Connection = oraConnect; oracleCom.CommandText = "Select m_content, f_extension From " + Session["tableNameIns"] +
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仍在服务器上写入和运行 只是附带说明一下,使用
writealBytes
将字节数组写入文件要容易得多。
您不必担心锁和以这种方式处理对象
File.WriteAllBytes(FileName, file);
谢谢你的回答!我是这样做的:
Response.ContentType = "APPLICATION/OCTET-STREAM";
Response.AddHeader("Content-Disposition", @"attachment;filename=\" + initFileName);
Response.BinaryWrite(file);
其中:file-它是二进制类型的参数。initFileName-字符串类型文件名。
只有3行)