I use paperclip to add a file to my model.
I want to use the new feature of firefox 3.6, xhr.sendAsBinary
, to send a file with an ajax request.
I finally made it work!
my javascript sending file looks like this
send : function() {
try {
var xhr = new XMLHttpRequest;
//var url = this.form.action;
var url = '/photos';
var boundary = this.generateBoundary();
var contentType = "multipart/form-data; boundary=" + boundary;
this.filesToUpload.forEach(function(file, index, all) {
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", contentType);
for (var header in this.headers) {
xhr.setRequestHeader(header, headers[header]);
}
var CRLF = "\r\n";
var request = "--" + boundary + CRLF;
request += 'Content-Disposition: form-data; ';
request += 'name="' + 'photo[name]' + '"' + CRLF + CRLF;
request += file.name + CRLF;
request += "--" + boundary + CRLF;
request += 'Content-Disposition: form-data; ';
request += 'name="' + 'photo[photo]' + '"; ';
request += 'filename="'+ file.fileName + '"' + CRLF;
request += "Content-Type: application/octet-stream" + CRLF + CRLF;
request += file.value + CRLF;
request+= "--" + boundary + "--" + CRLF;
xhr.sendAsBinary(request);
});
// finally send the request as binary data
//xhr.sendAsBinary(this.buildMessage(this.filesToUpload, boundary));
} catch(e) {
alert('send Error: ' + e);
}
}
now Paperclip handles the file as a normal input file