You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.8 KiB

  1. <html>
  2. <head>
  3. <title>Convert a file to a base64 request</title>
  4. <script type="text/javascript">
  5. function form_submit(e){
  6. console.log(e)
  7. var resultOutput = document.getElementById('resultOutput');
  8. var fileInput = document.getElementById('fileInput');
  9. var fieldInput = document.getElementById('fieldInput');
  10. makeRequestBase64(fileInput.files[0], fieldInput.value, function(err, result){
  11. resultOutput.value = result;
  12. });
  13. return false;
  14. }
  15. function makeRequestBase64(file, fieldName, cb){
  16. var boundary = '\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/';
  17. var crlf = "\r\n";
  18. var reader = new FileReader();
  19. reader.onload = function(e){
  20. var body = '';
  21. body += '--' + boundary + crlf;
  22. body += 'Content-Disposition: form-data; name="' + fieldName + '"; filename="' + escape(file.name)+ '"' + crlf;
  23. body += 'Content-Type: ' + file.type + '' + crlf;
  24. body += 'Content-Transfer-Encoding: base64' + crlf
  25. body += crlf;
  26. body += e.target.result.substring(e.target.result.indexOf(',') + 1) + crlf;
  27. body += '--' + boundary + '--';
  28. var head = '';
  29. head += 'POST /upload HTTP/1.1' + crlf;
  30. head += 'Host: localhost:8080' + crlf;
  31. head += 'Content-Type: multipart/form-data; boundary=' + boundary + '' + crlf;
  32. head += 'Content-Length: ' + body.length + '' + crlf;
  33. cb(null, head + crlf + body);
  34. };
  35. reader.readAsDataURL(file);
  36. }
  37. </script>
  38. </head>
  39. <body>
  40. <form action="" onsubmit="return form_submit();">
  41. <label>File: <input id="fileInput" type="file" /></label><br />
  42. <label>Field: <input id="fieldInput" type="text" value="file" /></label><br />
  43. <button type="submit">Ok!</button><br />
  44. <label>Request: <textarea id="resultOutput" readonly="readonly" rows="20" cols="80"></textarea></label><br />
  45. </form>
  46. <p>
  47. Don't forget to save the output with windows (CRLF) line endings!
  48. </p>
  49. </body>
  50. </html>