http:/
を参考にしたのだけど、微妙にmultipartのboundaryの作りが間違っているぽくて、
修正が必要だったのでメモっておく。
PHPではあのまま動くのかもしれないけど、少なくともrubyのcgiじゃだめでした。
修正版は以下。
- 1function upload(event) {
- 2
- 3 var data = event.dataTransfer;
- 4
- 5 var boundary = '------multipartformboundary' (new Date).getTime();
- 6 var dashdash = '--';
- 7 var crlf = '\r\n';
- 8
- 9 /* Build RFC2388 string. */
- 10 var builder = '';
- 11
- 12 var xhr = new XMLHttpRequest();
- 13
- 14 /* For each dropped file. */
- 15 for (var i = 0; i < data.files.length; i ) {
- 16 var file = data.files[i];
- 17
- 18 /* Write boundary. */
- 19 builder = dashdash;
- 20 builder = boundary;
- 21 builder = crlf;
- 22
- 23 /* Generate headers. */
- 24 builder = 'Content-Disposition: form-data; name="user_file"';
- 25 if (file.fileName) {
- 26 builder = '; filename="' file.fileName '"';
- 27 }
- 28 builder = crlf;
- 29
- 30 builder = 'Content-Type: application/octet-stream';
- 31 builder = crlf;
- 32 builder = crlf;
- 33
- 34 /* Append binary data. */
- 35 builder = file.getAsBinary();
- 36 builder = crlf;
- 37 }
- 38
- 39 /* Mark end of the request. */
- 40 builder = dashdash;
- 41 builder = boundary;
- 42 builder = dashdash;
- 43 builder = crlf;
- 44
- 45 xhr.open("POST", "upload.php", true);
- 46 xhr.setRequestHeader('content-type', 'multipart/form-data; boundary='
- 47 boundary);
- 48 xhr.sendAsBinary(builder);
- 49
- 50 xhr.onload = function(event) {
- 51 /* If we got an error display it. */
- 52 if (xhr.responseText) {
- 53 alert(xhr.responseText);
- 54 }
- 55 $("#dropzone").load("list.php?random=" (new Date).getTime());
- 56 };
- 57
- 58 /* Prevent FireFox opening the dragged file. */
- 59 event.stopPropagation();
- 60
- 61}
ついでにrubyでは
でok。
- 1cgi = CGI.new
- 2cgi.params["user_file"].each do |file|
- 3 do_something(file)
- 4end
そしてこれを貼り付けるために
http:/
を改造して導入してみた。なんか今週末はコードばっかり書いてるなあ。