简体   繁体   中英

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. 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. The acrobat reader doesn't open and i don't see acroRd32.exe in the taskmgr manager. I feel it is permission issue because i use process.start() which may not allow in the server but i dont see error messages. 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. 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 .
Therefore, Process.Start starts a process on the server, not your computer.

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.

you don't have permissions to do that from the ASP.Net worker process. you need impersonation:

ASP.NET Impersonation

How To: Use Impersonation and Delegation in 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.

Serving Dynamic Content with HTTP Handlers

Or if you are useing ASP.NET.MVC:

ASP.NET MVC - How do I code for PDF downloads?

This code works today (2022) in IIS 10, Windows Server 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
                
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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