4

Is there a way to put a file from the server into an file type input tag via javascript? Just the way i change the value of a text type input, i want to insert a file to the file type input.

I already tried to do things like:

var _file = new File(); document.getElementById('fileInputId').files[0] = _file; 

but it seems that the filelist property of input is protected and i cannot change the file objects inside it.

I want to bring a file from server and load it into the file input.

Is there a way to achieve this issue?

Thank y'all!

2
  • 2
    file input is used to send local file to the server, why you want server file there? Commented Apr 1, 2015 at 14:36
  • 4
    xyproblem.info Commented Apr 1, 2015 at 14:37

1 Answer 1

0

In case you need it for testing (cypress):

cy.get('input[type=file]').then($input => cy.fixture(fname) .then(Cypress.Blob.base64StringToBlob) .then(blob => { const file = new File([blob], fname, {type}); const dt = new DataTransfer; dt.items.add(file); $input[0].files = dt.files; }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.