[英]System.Diagnostics.Process.Start doesn't work in Default.aspx.cs
[英]System.Diagnostics.Process.Start() doesn't work in the web server
我有一個 C# asp.net web 頁面,它從 Mysql 數據庫(存儲為 longblob 格式)讀取 PDF 文件並打開它。 它在我的本地主機上工作; 該頁面從數據庫中讀取文件並使用 acrobat 閱讀器打開,但在我部署該頁面后它在測試服務器中不起作用。 acrobat 閱讀器打不開,我在 taskmgr 管理器中也沒有看到 acroRd32.exe。 我覺得這是權限問題,因為我使用 process.start() ,這在服務器中可能不允許,但我沒有看到錯誤消息。 如果有權限需要在服務器端做; 誰能給我指出方向?
謝謝你。
MySqlDataReader Reader = null;
connection.Open();
MySqlCommand command = new MySqlCommand("Select Image, File_Type, File_Name from table where ImageID = " + ImageID, connection);
Reader = command.ExecuteReader();
if (Reader.Read())
{
byte[] buffer = (byte[])Reader["Image"];
System.IO.MemoryStream stream1 = new System.IO.MemoryStream(buffer, true);
stream1.Write(buffer, 0, buffer.Length);
String fileName = Reader["File_Name"].ToString();
String dirName = "C:\\thefolder\\";
if (!Directory.Exists(dirName))
{
// if not then create
Directory.CreateDirectory(dirName);
}
if (File.Exists(dirName+fileName))
File.Delete(dirName + fileName);
Directory.CreateDirectory(Path.GetDirectoryName(Reader["File_Name"].ToString()));
using (Stream file = File.Create(dirName + fileName))
{
file.Write(buffer, 0, buffer.Length);
}
Process process = new Process();
process.StartInfo.FileName = "AcroRd32.exe";
process.Start();
}
感謝您的幫助,我可以通過回復發送 pdf 內容。 這是代碼
//Process process = new Process();
//process.StartInfo.FileName = "AcroRd32.exe";
//process.Start();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment");
Response.TransmitFile(dirName + fileName);
Response.End();
您的C#代碼在服務器上運行。
因此, Process.Start
在服務器而不是您的計算機上啟動進程。
從根本上說,您無法直接在客戶端上啟動流程。
但是,如果您在HTTP響應中使用正確的Content-Type
提供PDF,則瀏覽器將在PDF查看器中打開它。
您無權從ASP.Net工作進程執行此操作。 你需要冒充:
沒有徹底閱讀問題...如果您不想在服務器上啟動進程,則可以使用模擬。 否則,您應該從IIS提供此文件 - 以允許用戶下載它。
或者如果您使用的是ASP.NET.MVC:
此代碼今天(2022 年)在 IIS 10、Windows 服務器 2019 中有效:
string strFileExePath = ConfigurationManager.AppSettings["Audio:ffmpeg.Directory"].ToString();
string strFileExe = ConfigurationManager.AppSettings["Audio:ffmpeg.Directory"].ToString() + "ffmpeg.exe";
// http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.useshellexecute.aspx
// http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(strFileExe);
psi.UseShellExecute = false;
psi.RedirectStandardOutput = false;
psi.RedirectStandardInput = true; // avoid hang, later close this...
psi.RedirectStandardError = false;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.CreateNoWindow = false; // If the UseShellExecute property is true or the UserName and Password properties are not null, the CreateNoWindow property value is ignored and a new window is created.
psi.ErrorDialog = false;
psi.WorkingDirectory = strFileExePath;
// argumentos
psi.Arguments = $"-y -i \"{fileWithPath}\" -aq 8 -ac 1 -f s16le -ar 8000 -acodec pcm_s16le -vol 500 \"{fileOutputWithPath}\"";
// Start the process
using (Process process = Process.Start(psi))
{
// http://csharptest.net/321/how-to-use-systemdiagnosticsprocess-correctly/
process.StandardInput.Close(); // no wait for input, avoid hang
// process.WaitForExit(); can produce request timeout
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.