0

I'm trying to convert a doc to html and this is the code I use. The problem is that I have no exception but I did not create the file. I've tried various alternatives but do not know how to proceed.

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        //check if file is ok
        if (file != null && file.ContentLength > 0)
        {

            var path = Path.Combine(Server.MapPath("~/App_Data/"),
                                    System.IO.Path.GetFileName(file.FileName));
            file.SaveAs(path);

            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = ("soffice.exe");
            psi.Arguments = string.Format("--headless --convert-to htm:HTML --outdir " + Server.MapPath("~/App_Data/batch/") + " \"{0}\"", path);
            psi.UseShellExecute = false;
            Process proc = new Process();
            proc.StartInfo = psi;
            proc.Start();
            proc.WaitForExit();
        }
        return View();
    }

I've updated the code :

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        //check if file is ok
        if (file != null && file.ContentLength > 0)
        {

            var path = Path.Combine(Server.MapPath("~/App_Data/"),
                                    System.IO.Path.GetFileName(file.FileName));
            file.SaveAs(path);

            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = Path.Combine(Server.MapPath("~/App_Data/LOP/App/libreoffice/program/"), "soffice.exe");
            psi.Arguments = string.Format("--headless --convert-to htm:HTML --outdir " + Server.MapPath("~/App_Data/batch/") + " \"{0}\"", path);
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            Process proc = new Process();
            proc.StartInfo = psi;
            proc.Start();
           string myString = proc.StandardOutput.ReadToEnd();
            Console.WriteLine(myString);
            proc.WaitForExit();
            var exitCode = proc.ExitCode;
        }
        return View();
    }

But Exitcode still 0 and myString is empty :(

--SOLUTION-- All code are ok! a process of libroffice was left hanging in memory and then every other request appending without give a results, the strange thing is that libreoffice continues to give exitcode 0 to all process in memory

tereško
  • 58,060
  • 25
  • 98
  • 150
RDM
  • 13
  • 6

2 Answers2

1

You can either query the ExitCode property on your Process instance after the process exits to check if the process executed without errors or you can read what's happening in the console from the StandardOutput property (however make sure to set ProcessStartInfo.RedirectStandardOutput to true and ProcessStartInfo.UseShellExecute to false as it is advised on MSDN http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.process.standardoutput).

  • I've set all but the return value is zero and into console nothing appears – RDM May 14 '14 at 13:45
  • Are you sure the arguments that you're providing to soffice.exe are valid? I'm not seeing all of these arguments as part of the tool's documentation. Check https://wiki.openoffice.org/wiki/Framework/Article/Command_Line_Arguments – Joe Zeitouny May 14 '14 at 13:56
  • Yes they are ok! i've saved a batch file with the same commands and if i launch is ok! I'm using libreoffice. – RDM May 14 '14 at 14:00
0

The answer to problem will be better understood by getting the output of your process. On your "psi" variable, set "RedirectStandardOutput = true" before you call "proc.Start();" then to access the output, add a line like the one below and set a breakpoint on it (or the line after it).

string output = proc.StandardOutput.ReadToEnd();

For more info, check this existing post below, look at the accepted answer ;-)

Capturing the console output

Community
  • 1
  • 1
RussellEast
  • 153
  • 8