-2

I need to append a specific file to my FormData object using JavaScript.

var form = new FormData();
form.append("input", '{ ... some json ... }');
form.append("files", "/C:/My/Path/File.txt");

I dont want to select the file through a input button.

All examples that i founded do this:

var form = new FormData();
form.append("input", '{ ... some json ... }');
form.append("files", input.Files[0], "/C:/My/Path/File.txt");
        

So i think that i have to load that file into a blob or file object. But i dont get it.

Hoy can i do it? Thanks!

5
  • i think that way.. var blob = new Blob([content], { type: "text/xml"}); formData.append("webmasterfile", blob); you can check docs here developer.mozilla.org/en-US/docs/Web/API/FormData/append Commented Oct 18, 2021 at 23:35
  • 2
    "I dont want to select the file through a input button.": So you're asking the user to trust you to not upload anything else from their computer. There has to be some user interaction for security purposes. Commented Oct 18, 2021 at 23:43
  • 1
    To be clear: you cannot do this. A website could grab arbitrary files from the user's hard drive this way. Commented Oct 18, 2021 at 23:45
  • I understand the security issues. I need to automate an api call. If i cant do this, should i try another language? Commented Oct 18, 2021 at 23:50
  • 2
    The language doesn't matter. The browser won't allow you to load a file from a local system without user authorisation. Commented Oct 18, 2021 at 23:54

1 Answer 1

1

Acoording to @Andy comment and to this post https://stackoverflow.com/a/3014623/8037813, effectively it is not possible to do this by security issues.

Dont lose your time trying to do it.

Thanks.

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.