I am trying to fake a file upload without actually using a file input from the user. The file's content will be dynamically generated from a string.
Is this possible? Have anyone ever done this before? Are there examples/theory available?
To clarify, I know how to upload a file using AJAX techniques using a hidden iframe and friends - the problem is uploading a file that is not in the form.
I am using ExtJS, but jQuery is feasible as well since ExtJS can plug into it (ext-jquery-base).
It is like a "multipart/form-data" upload without a form. You can also upload the file directly as content inside the body of the POST request using xmlHttpRequest like this: var xmlHttpRequest = new XMLHttpRequest(); var file = ...file handle... var fileName = ...file name...
You could add a new <input type="file"> whenever you finished uploading the previous files and hide the previous input. This way you keep adding a new input every time you want to add more files and prevent the previous input from being overwritten.
If you don't need support for older browsers you can use the FormData Object, which is part of the File API:
var formData = new FormData(); var blob = new Blob(['Lorem ipsum'], { type: 'plain/text' }); formData.append('file', blob,'readme.txt'); var request = new XMLHttpRequest(); request.open('POST', 'http://example.org/upload'); request.send(formData);
File Api is supported by all current browsers (IE10+)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With