I understand that the <input type="file">
for the most part cannot be manipulated by javascript for security reasons, but is it possible if I have a File object?
I have a drag area on a page, that I can get a File object out of. I don't want to use the xhr.sendAsBinary, I'd rather just upload from a regular form with the target set as a frame.
I've tried doing something like
var select = document.getElementById('upload_1');
select.files[0] = myFile;
myForm.submit();
Is there a way to do this?
UPDATE
It seems like you want to take the
File
object from thedrop
event and assign it to the<input>
element. Unfortunately, you can't do that. Only the user can select files; you can't dynamically change the files which will be uploaded because browsers deny JavaScript this ability for security reasons.Since you said you have a drag area, I'm assuming you are using and targeting a browser that supports drag and drop. Note that not all browsers support drag and drop so my example here is limited to such browsers.
With drag and drop, you can get the
File
object out of the drop event and you don't need aninput
element.Note that this isn't 100% browser compatible. Some browsers don't support file uploading through
XMLHttpRequest()
. If you want to support those browsers, then you have to do something different.Finally, my example doesn't consider using forms. If you want a form-based approach, please let me know. I can help you with that as well :)
Let me know if you have any questions.