Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

asp.net

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();
like image 306
windforceus Avatar asked Jan 17 '23 16:01

windforceus


1 Answers

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.

like image 61
SLaks Avatar answered Jan 25 '23 22:01

SLaks