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.

formdata.js 1.2KB

7 months ago
12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * Emulate FormData for some browsers
  3. * MIT License
  4. * (c) 2010 François de Metz
  5. */
  6. (function(w) {
  7. if (w.FakeFormData)
  8. return;
  9. function FormData() {
  10. this.fake = true;
  11. this.boundary = "--------FormData" + Math.random();
  12. this._fields = [];
  13. }
  14. FormData.prototype.append = function(key, value) {
  15. this._fields.push([key, value]);
  16. }
  17. FormData.prototype.toString = function() {
  18. var boundary = this.boundary;
  19. var body = "";
  20. this._fields.forEach(function(field) {
  21. body += "--" + boundary + "\r\n";
  22. // file upload
  23. if (field[1].name) {
  24. var file = field[1];
  25. body += "Content-Disposition: form-data; name=\""+ field[0] +"\"; filename=\""+ file.name +"\"\r\n";
  26. body += "Content-Type: "+ file.type +"\r\n\r\n";
  27. body += file.getAsBinary() + "\r\n";
  28. } else {
  29. body += "Content-Disposition: form-data; name=\""+ field[0] +"\";\r\n\r\n";
  30. body += field[1] + "\r\n";
  31. }
  32. });
  33. body += "--" + boundary +"--";
  34. return body;
  35. }
  36. w.FakeFormData = FormData;
  37. })(window);