Miguel
Miguel

Reputation: 45

How to manipulate Excel file on the client side

I´m new to php and javascript, so i'm not even sure i can properly expose my case.

I have a php form where the client selects options on his browser.

On submit, a need the client browser to download an excel file from another domain (i have no control on the 2nd server response).

It must be done on the client side because only the user has access to the 2nd domain (inside a on-premisses network or from the internet with a VPN).

I managed to force the download with the following javascript:

function download(dataurl,filename) {
    var a = document.createElement("a");
    a.href = dataurl;
    a.download = filename;
    a.click();
    window.URL.revokeObjectURL(dataurl);
    a.remove();
}
download(url,filename);

The filename does nothing, as the file is saved with the name sent by the server.

After the file is downloaded, i still need to rename and move it to a specific network directory (only acessible by the client).

I can´t use js fetch because the browser blocks the download with CORS.

I'm not even sure if this is possible due to security reasons.

Any help would be much appreciated.

Upvotes: 0

Views: 370

Answers (3)

Andrew Paul
Andrew Paul

Reputation: 162

As a dirty workaround, how about generating a .bat file with the appropriate DOS copy commands? If your user is happy to download and execute the file, it might do the job!

I've done something similar with a web app that generates commands for a local macro program.

For example: You might save the following to copy the file to the correct directory

copy c:\\path\to\download\file.xls n:\\path\to\networked\destination

or you could make use of built in windows commands to handle the file download

curl.exe -0 c:\\path\to\download\file.xls https://domain_with_file.com/file.xls

This isn't an ideal way of doing things though, and it looks as if any batch files downloaded might be flagged up as potential virus threats.

Upvotes: 0

Miguel
Miguel

Reputation: 45

I think i found a solution (IE only with appropriate internet options settings):

function test() {
    /* global ActiveXObject*/
    var Excel = new ActiveXObject("Excel.Application");
    Excel.Visible = true; //optional
    Excel.Workbooks.Open("C:\\Users\\user1\\Downloads\\file1.xlsx");
}

From here, i believe i can do pretty much what i want.

Upvotes: 0

ADyson
ADyson

Reputation: 61904

"i still need to rename and move it to a specific network directory"

...you can't do that from JavaScript, JavaScript running in a browser has no access to the filesystem of the device it's running on, for security reasons.

All you can do is give the instructions about what they need to do. If that's not satisfactory, then you would need to implement this project as a desktop application, rather than a web application.

Upvotes: 1

Related Questions