简体   繁体   English

System.Diagnostics.Process.Start() 在 web 服务器中不起作用

[英]System.Diagnostics.Process.Start() doesn't work in the web server

I have a C# asp.net web page which reads PDF file from Mysql database (which stores as longblob format) and open it.我有一个 C# asp.net web 页面,它从 Mysql 数据库(存储为 longblob 格式)读取 PDF 文件并打开它。 It works in my Localhost;它在我的本地主机上工作; the page reads the file from database and open with acrobat reader but it doesn't work in the testing server after i deploy the page.该页面从数据库中读取文件并使用 acrobat 阅读器打开,但在我部署该页面后它在测试服务器中不起作用。 The acrobat reader doesn't open and i don't see acroRd32.exe in the taskmgr manager. acrobat 阅读器打不开,我在 taskmgr 管理器中也没有看到 acroRd32.exe。 I feel it is permission issue because i use process.start() which may not allow in the server but i dont see error messages.我觉得这是权限问题,因为我使用 process.start() ,这在服务器中可能不允许,但我没有看到错误消息。 If there are permissions needs to be done in server;如果有权限需要在服务器端做; can anyone kindly points me the direction?谁能给我指出方向?

Thank You.谢谢你。

Here are my code:这是我的代码:

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();                    
}

Thanks for your help, i am able to send pdf content via response.感谢您的帮助,我可以通过回复发送 pdf 内容。 Here is the code这是代码

//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();

Your C# code runs on the server . 您的C#代码在服务器上运行。
Therefore, Process.Start starts a process on the server, not your computer. 因此, Process.Start在服务器而不是您的计算机上启动进程。

It is fundamentally impossible for you to start a process directly on the client. 从根本上说,您无法直接在客户端上启动流程。

However, if you serve the PDF in the HTTP response (with the correct Content-Type ), the browser will open it in a PDF viewer. 但是,如果您在HTTP响应中使用正确的Content-Type提供PDF,则浏览器将在PDF查看器中打开它。

you don't have permissions to do that from the ASP.Net worker process. 您无权从ASP.Net工作进程执行此操作。 you need impersonation: 你需要冒充:

ASP.NET Impersonation ASP.NET模拟

How To: Use Impersonation and Delegation in ASP.NET 2.0 如何:在ASP.NET 2.0中使用模拟和委派


Hadn't read the question thoroughly... If you won't to start a process on the server , you can use impersonation. 没有彻底阅读问题...如果您不想在服务器上启动进程,则可以使用模拟。 Otherwise you should serve this file from the IIS - to allow the user to download it. 否则,您应该从IIS提供此文件 - 以允许用户下载它。

Serving Dynamic Content with HTTP Handlers 使用HTTP处理程序提供动态内容

Or if you are useing ASP.NET.MVC: 或者如果您使用的是ASP.NET.MVC:

ASP.NET MVC - How do I code for PDF downloads? ASP.NET MVC - 如何编写PDF下载代码?

This code works today (2022) in IIS 10, Windows Server 2019:此代码今天(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM