2

I have an array of File's:

var images = [];

How can I create a valid input and sent to server using Javascript such as:

<input type="file" multiple="multiple" value="{my images array}">

It needs to be done via JS with AJAX. The HTML input does not exist, so i need to create it in js. The question how to assign my images array to input's value attribute assuming that images array is not empty?

6
  • You're asking how to create the element as HTML by using JS and also asking how to post that to the server? So do you already know how to use a form or not? Because if not, that needs to be part of the answer. Also does that element already exist? Does a form exist? Or is this purely JS + AJAX? images is empty. Should it be? If so where does {my images array} come from? Commented Aug 27, 2015 at 12:18
  • @Popnoodles it's purely js with ajax. the element does not exist - that what i was asking. how can i assign my js array to input value Commented Aug 27, 2015 at 12:26
  • 1
    Ok .I would recommend editing the question before it gets closed for being unclear. Commented Aug 27, 2015 at 12:27
  • So are we also to assume that images is not an empty array? Commented Aug 27, 2015 at 12:28
  • And can we see the jQuery that you have already so that we can apply the solution to it? Commented Aug 27, 2015 at 12:28

2 Answers 2

2

So I found the way to do it - to use input's files property, like

var input = $('<input/>', {
                type: 'file',
                multiple: 'multiple',
            })
            input.files = images;
Sign up to request clarification or add additional context in comments.

Comments

-1

does this help?

inputElement.onchange = function(event) {
   images  = inputElement.files;
}

3 Comments

not what i was looking for. it should be inverted. in that case it will work ?
I'm not sure you can set it the way you're looking for, but I could be wrong
if you don't set multiple when creating the object this would only get the one file the user was able to select. it will not allow the user to select multiple files

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.