Run a command line utility in ASP.NET

I need some tips on using the command line utility from a C # / ASP.NET web application.

I found a third-party utility for converting files to CSV format. The utility works fine and can be used from the command line.

I searched the Internet for examples of how to run a command-line utility, and found this example.

The problem is that this is not very good. When I try to use the sample code with my utility, I get an invitation asking me to install the utility on the client machine. This is not what I want. I do not want the user to see what is happening in the background.

Is it possible to execute a server command and process the file?

Any help would be greatly appreciated.

+4
source share
2 answers

In the past, I have done something like this several times, and here is what worked for me:

Create an IHttpHandler implementation (easiest to do as an .ashx file) to handle conversion. Inside the handler, use System.Diagnostics.Process and ProcessStartInfo to start the command line utility. You should be able to redirect standard output to the output stream of your HTTP response. Here is the code:

public class ConvertHandler : IHttpHandler { #region IHttpHandler Members bool IHttpHandler.IsReusable { get { return false; } } void IHttpHandler.ProcessRequest(HttpContext context) { var jobID = Guid.NewGuid(); // retrieve the posted csv file var csvFile = context.Request.Files["csv"]; // save the file to disk so the CMD line util can access it var filePath = Path.Combine("csv", String.Format("{0:n}.csv", jobID)); csvFile.SaveAs(filePath); var psi = new ProcessStartInfo("mycsvutil.exe", String.Format("-file {0}", filePath)) { WorkingDirectory = Environment.CurrentDirectory, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }; using (var process = new Process { StartInfo = psi }) { // delegate for writing the process output to the response output Action<Object, DataReceivedEventArgs> dataReceived = ((sender, e) => { if (e.Data != null) // sometimes a random event is received with null data, not sure why - I prefer to leave it out { context.Response.Write(e.Data); context.Response.Write(Environment.NewLine); context.Response.Flush(); } }); process.OutputDataReceived += new DataReceivedEventHandler(dataReceived); process.ErrorDataReceived += new DataReceivedEventHandler(dataReceived); // use text/plain so line breaks and any other whitespace formatting is preserved context.Response.ContentType = "text/plain"; // start the process and start reading the standard and error outputs process.Start(); process.BeginErrorReadLine(); process.BeginOutputReadLine(); // wait for the process to exit process.WaitForExit(); // an exit code other than 0 generally means an error if (process.ExitCode != 0) { context.Response.StatusCode = 500; } } } #endregion } 
+9
source

The team works on the server side. The server runs any code. The code in the example that you give the works. You just need to make sure that the utility is configured correctly on the server and that you have permissions on the directory / file.

+2
source

All Articles