4

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();
windforceus
  • 193
  • 2
  • 4
  • 16
  • http://stackoverflow.com/editing-help#code – SLaks Jan 26 '12 at 20:11
  • no error, The web page runs and acroRd32.exe doesnt' run. All i want is user is able to see the PDF file which stores in the database when they click the web page. If they need to save the file in the their own machine first and open it. that is fine too. – windforceus Jan 26 '12 at 20:23

3 Answers3

7

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Hi SLaks , can you be a little more specific about how "serve the PDF in the HTTP response "? – windforceus Jan 26 '12 at 20:17
  • Send the content of the PDF in the HTTP response. You can do that by writing the buffer to the `Response` object. You'll also need to set `Response.ContentType` to `application/pdf` to tell the browser what you're giving it. – SLaks Jan 26 '12 at 20:21
  • @windforceus Instead of saving the file bytes to the disk, you send them down the `Response` stream; after setting the correct headers and assuring no incorrect ones exist. – Andrew Barber Jan 26 '12 at 20:21
  • I will try to send the content of the PDF in the HTTP response and let you guys know. Thanks – windforceus Jan 26 '12 at 20:48
2

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?

Community
  • 1
  • 1
AK_
  • 7,981
  • 7
  • 46
  • 78
  • And you should only do this if you intend to start it **on the webserver** – antiduh Jan 26 '12 at 20:17
  • It's more probable that he doesn't have ASP.NET tracing on, then that he configured the user ASP.NET user to be able to start processes, because be default it can't. at least on IIS 7 – AK_ Jan 26 '12 at 20:24
  • Thanks Hellfrost, seems like http handlers is a better way then i can't need to change IIS and server setting which i would like to avoid because we have other web application hosting in the same server. I will try it to see if it works – windforceus Jan 26 '12 at 20:46
0

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
                
            }