Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  167] [ 5]  / answers: 1 / hits: 55037  / 15 Years ago, thu, february 4, 2010, 12:00:00

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).


More From » file-upload

 Answers
55

Why not just use XMLHttpRequest() with POST?



function beginQuoteFileUnquoteUpload(data)
{
var xhr = new XMLHttpRequest();
xhr.open(POST, http://www.mysite.com/myuploadhandler.php, true);
xhr.setRequestHeader(Content-type, application/x-www-form-urlencoded);
xhr.onreadystatechange = function ()
{
if (xhr.readyState == 4 && xhr.status == 200)
alert(File uploaded!);
}
xhr.send(filedata=+encodeURIComponent(data));
}


The handler script at the server just writes the file data to a file.



EDIT

File upload is still a http post with a different content type. You can use this content type and separate your content with boundaries:



function beginQuoteFileUnquoteUpload(data)
{
// Define a boundary, I stole this from IE but you can use any string AFAIK
var boundary = ---------------------------7da24f2e50046;
var xhr = new XMLHttpRequest();
var body = '--' + boundary + 'rn'
// Parameter name is file and local filename is temp.txt
+ 'Content-Disposition: form-data; name=file;'
+ 'filename=temp.txtrn'
// Add the file's mime-type
+ 'Content-type: plain/textrnrn'
+ data + 'rn'
+ boundary + '--';

xhr.open(POST, http://www.mysite.com/myuploadhandler.php, true);
xhr.setRequestHeader(
Content-type, multipart/form-data; boundary=+boundary

);
xhr.onreadystatechange = function ()
{
if (xhr.readyState == 4 && xhr.status == 200)
alert(File uploaded!);
}
xhr.send(body);
}


If you want to send additional data, you just separate each section with a boundary and describe the content-disposition and content-type headers for each section. Each header is separated by a newline and the body is separated from the headers by an additional newline. Naturally, uploading binary data in this fashion would be slightly more difficult :-)



Further edit: forgot to mention, make sure whatever boundary string isn't in the text file that you're sending, otherwise it will be treated as a boundary.


[#97669] Sunday, January 31, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ednakarolinal

Total Points: 187
Total Questions: 106
Total Answers: 118

Location: Saint Helena
Member since Mon, Jan 16, 2023
1 Year ago
;