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?

marknorkin
  • 3,904
  • 10
  • 46
  • 82
  • 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? – Popnoodles Aug 27 '15 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 – marknorkin Aug 27 '15 at 12:26
  • 1
    Ok .I would recommend editing the question before it gets closed for being unclear. – Popnoodles Aug 27 '15 at 12:27
  • So are we also to assume that images is not an empty array? – Popnoodles Aug 27 '15 at 12:28
  • And can we see the jQuery that you have already so that we can apply the solution to it? – Popnoodles Aug 27 '15 at 12:28
  • You need to see the contents of what input:file actually uploads. I'm guessing you already tried putting the array in value. http://stackoverflow.com/questions/15726439/how-to-upload-multiple-files-using-one-file-input-element – Popnoodles Aug 27 '15 at 12:34

2 Answers2

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;
marknorkin
  • 3,904
  • 10
  • 46
  • 82
-1

does this help?

inputElement.onchange = function(event) {
   images  = inputElement.files;
}
stackoverfloweth
  • 6,669
  • 5
  • 38
  • 69