0

I have this code :

var link= document.createElement("a");
link.setAttribute("download",name);
link.href=uri;
link.target="_blank";
document.body.appendChild(link);
link.click();
link.remove();

Here I am passing -

url ="M:/folderfname/filename.xlsx";
name = "filename.xlsx"

I am getting this error:

Not Allowed To Load Local Resource file://M:/folderfname/filename.xlsx

I am not getting how can I resolve this in react. Or is there any alternative to download file ?

Note: this is not about downloading a single file which I can import and download. FileName to download getting generated dynamically.

1
  • 1
    First off, you should not use direct DOM manipulation like createElement in React code if you can. Secondly, as the error states, you cannot use local filepaths. Put the file into the public folder of your React app and reference it from there. Like /filename.xlsx Commented Oct 11, 2021 at 8:01

1 Answer 1

1

It is not possible to access the local file system in your browser app without explicit permission. One of the browser's primary functions is to prevent this.

You can take a look at the File System Access API for progress in this area. Alternatively, you can operate a web server on your device and communicate with it in your app, using it as a proxy to access the file system. Because you are already writing JavaScript, I suggest that you explore Deno and Node for the server approach.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.