简体   繁体   中英

No application is associated with the specified file for this operation

I am currently using Process.start() in a controller method in my ASP MVC site. This is a small intranet app, so security beyond the basic stuff we have set up is not a major concern.

The users will be able to enter filepath for the attachments that is the location of a shared folder on our LAN. Then, when an edit page for each account is brought up, the page will display a hyperlink to the attachment. The user should be able to click on the hyperlink and open up the file, not a copy of the file or download a copy of the file, so that they may make and save edits.

My first attempt at this was to create the following ActionLink that sends the file's location to the controller.

<div class="M-editor-label" style="width: 400px;word-wrap: break-word;">
    @Html.ActionLink(Model.Attachment, "OpenAttachment", "BankListMaster", new { location = Model.Attachment }, new { @target = "_blank" })
</div>

The controller then uses Process.Start() to open the file like so

    public void OpenAttachment(string location)
    {
        try
        {
            EventLog.WriteEntry("Monet", "Inside OpenAttachment |URL| " + location);

            Process proc = new Process();
            proc.StartInfo = new ProcessStartInfo(location);
            proc.Start();
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry("Monet", ex.StackTrace);
        }
    }

This worked fine when I was testing locally, but the server does not have a copy of Office installed and so I get this error in the catch block:

No application is associated with the specified file for this operation.  at 
System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at Monet.Controllers.BankListMasterController.OpenAttachment(String location) in 
C:\Projects\sym_Monet_INT\Monet\Controllers

Can anyone either point me in the direction of a fix for the method I'm using or in the direction of a better one that will accomplish the same goal?

The Process.Start() method will not work for what you are trying to accomplish. It is not possible for a controller which is running on the server to start a process on the end user's system. In testing, this only worked because the server and the client happen to be the same machine. The only code running on the client side of the app is the web browser, and your only control point for the web browser is JavaScript. You can create a JavaScript function, similar to the following:

<script type="text/javascript">
    function OpenFile(filePath) {
        var sh = new ActiveXObject("WScript.Shell");
        var fullFileName = "file://" + filePath;
        sh.run("\"" + fullFileName + "\"", 1);
    }
</script>

and then call this in the onclick method of a button like this:

<button onclick="OpenFile('c:/Users/UserName/Desktop/Test.txt');">Open</button>

you can then use Razor View syntax to replace the path in the button method with the appropriate path. Also note, this uses ActiveX, so you will need to make sure that ActiveX is allowed in the client browser.

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