skip to Main Content

I’m using VueJS/Axios and have to implement a download file button.

My client requirement is when document is downloaded, it should automatically open with an appropriate program on the user’s computer. For example when it is a Docx file and if the machine have MS Word, then open the doc automatically just after it is download.

NOTE: Even though in Chrome there is the option to set always open files of this type, this is not an option. Have to consistently work in all major browsers by default.

My code looks like below

   await axiosCall(urlForFileContent).then(function (response)
    {
        const link = document.createElement('a');
        link.href = window.URL.createObjectURL(response.data);
        link.download = "TestFile.Docx";
        link.click();
        window.URL.revokeObjectURL(link.href)
    }

File can be a Docx, Pdf etc and the web service sets the correct MIME Type. I can see the download response contains correct "Content-Type" set when check in DevTools network section. e.g when it is a Docx,

Content-Type = application/vnd.openxmlformats-officedocument.wordprocessingml.document

Thought when have correct MIME type it will work, But no. Does anyone have an idea how to achieve this ?

2

Answers


  1. It is not possible. Even if it were, it would be a massive security risk, as the trusted site (whether by itself or by being hacked) could automatically download a visual basic script to your computer or a batch files, or any other type of malware that will automatically run automatically. You might be able to have a local program watch for downloads and open them, but you can’t do it from a webpage.

    Login or Signup to reply.
  2. using call %var% will open the filepath set to var, you can also use start or type.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search