简体   繁体   中英

Open Word/Excel file using ASP.NET MVC C#

I have a View with a button , when a click that button I open a word file using Microsoft Word :

JS

//CLICK EVENT
$('.openDocument').click(function () {
        $.ajax({
            url: "openFile",
            method: "GET",
            async: false,
            success: function (respuesta) {

            }
        });

    });

CONTROLLER

 [HttpGet]
   public void openFile()
   {
      Process.Start(@"C:\Users\user01\Downloads\Test_document.docx");
   }

I have 2 questions about this:

1) When I publish the project locally, I can't open the file again (only when I run it), why when it is published it does not open? (Only the Microsoft Word icon appears in the Task Manager)

2) Already published, how can I do it so that another PC can open the file (to be shown on the other PC) and not open it on the server PC?

Thanx

try this for question 1

public static Process CreateProcess(string exePath, string arguments = null, bool isHidden = true, string workingDirectory = null)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo
            {
                FileName = exePath,
                Arguments = arguments,
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                WorkingDirectory = workingDirectory ?? Path.GetDirectoryName(exePath)
            };

            if (isHidden)
                processStartInfo.CreateNoWindow = true;

            return new Process()
            {
                StartInfo = processStartInfo,
                EnableRaisingEvents = true
            };
        }

        public static void RunProcess(string exePath, string arguments = null, bool isHidden = true, string workingDirectory = null)
        {
            Process process = CreateProcess(exePath, arguments, isHidden, workingDirectory);
            if (process.Start())
            {
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                process.WaitForExit();
            }


        }

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