Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to POST a file (from File API) from drag and drop
    primarykey
    data
    text
    <p>I have a file-upload page where the file is selected via drag and drop. Because the file is dropped in (rather than using a traditional field), I have the file exposed to me through the javascript File API.</p> <p>I've been reading <a href="https://developer.mozilla.org/en/Using_files_from_web_applications" rel="nofollow">this article from Mozilla</a>, but I am not all that interested in asynchronously uploading it (as I have some other data that needs to be submitted with the file, but all the resources I have managed to find are doing it with XHR.</p> <p>Here's what I've managed to piece together:</p> <pre><code>function handleFileSelect(evt) { evt.toElement.className = ""; evt.stopPropagation(); evt.preventDefault(); var files = evt.dataTransfer.files; // FileList object. // files is a FileList of File objects. List some properties. var output = []; for (var i = 0, file; file = files[i]; i++) { var f = file; var reader = new FileReader(); reader.onload = function(e) { var bin = e.target.result; // bin is the binaryString }; reader.readAsBinaryString(file); var xhr = new XMLHttpRequest(); xhr.open("POST", "{% url micro-upload %}"); xhr.overrideMimeType('text/plain; charset=x-user-defined-binary'); xhr.sendAsBinary(bin); output.push('&lt;li&gt;&lt;strong&gt;Uploaded:&lt;/strong&gt;&lt;em&gt;', escape(f.name), '&lt;/em&gt; (', f.type || 'n/a', ') - ', f.size, ' bytes, last modified: ', f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a', '&lt;/li&gt;'); } document.getElementById('list').innerHTML = '&lt;ul&gt;' + output.join('') + '&lt;/ul&gt;'; } </code></pre> <p>However server side, Django does not like this. It complains about malformed POST data.</p> <p>Ideally, my solution I think would be this:</p> <ol> <li>User drag-and-drops file onto page</li> <li>File is added to a hidden file <code>&lt;input&gt;</code></li> <li>File is POSTed along with the rest of the form data.</li> </ol> <p>I am aware that most browsers let you drag files onto a file <code>&lt;input&gt;</code>, but I want to have a larger drop target with custom styling. </p>
    singulars
    1. This table or related slice is empty.
    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.
    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