Note that there are some explanatory texts on larger screens.

plurals
  1. POSplit multi-file post with Javascript before upload
    primarykey
    data
    text
    <p>I understand the HTML/PHP side of a multi-file upload, but I suppose what I'm after is a Javascript solution that separates the files array pre-post and individually sends the files to a separate PHP program to receive upload and success/fail feedback before continuing. (ie: files[0] -> POST -> Success -> files[1] -> POST -> Success ->etc...) </p> <p>Here is what I use now for a single file -</p> <pre><code> function upload(file){ var data = new FormData(); data.append("file", document.getElementById(file).files[0]); var xmlHTTP = new XMLHttpRequest(); xmlHTTP.open("POST", "uploader.php"); xmlHTTP.send(data); } </code></pre> <p>I realize that the easy way out would be to create multiple file fields, however what I'm trying to do is use <code>&lt;input type="file" multiple&gt;</code> from HTML5 to mass-select a list of files at a time. If I could only separate the files with Javascript, then it would be a simple matter of looping through the above script with an <code>onreadystatechange</code> reporting the success/fail each time.</p> <p>Any ideas?</p> <p>EDIT:<br> Forgive my tunnel vision, that was extremely simple! Here's my full code in case it helps someone else out. </p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script language="Javascript"&gt; function multiupload(file){ count = 0; maxcount = document.getElementById(file).files.length; alert(maxcount); upload(file,count); } function upload(file,count){ var data = new FormData(); data.append("file", document.getElementById(file).files[count]); var xmlHTTP = new XMLHttpRequest(); xmlHTTP.open("POST", "uploader.php"); xmlHTTP.onreadystatechange=function(){ if (xmlHTTP.readyState == 4 &amp;&amp; xmlHTTP.status == 200){ count = count+1; if(count&lt;maxcount){ upload(file,count); } } } xmlHTTP.send(data); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="result"&gt;&lt;/div&gt; &lt;form method="post" action=""&gt; &lt;input type="file" name="file" id="file" multiple&gt; &lt;input type="button" name="submit" value="Upload" onclick="multiupload('file');void(0);"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload