MyGit

henryluki/FormData

Fork: 13 Star: 22 (更新于 2024-11-05 05:09:42)

license: MIT

Language: CoffeeScript .

FormData support where window.FormData is undefined

GitHub网址

FormData

FormData support where window.FormData is undefined

About FormData

This is project was inspired by html5-formdata and it implements FormData with append and toString methods.

But unlike html5-formdata, it supports Blob and use FileReader and ArrayBuffer to convert Blob to string.

NPM Use

npm install formdata-emulate

How to use it ?

import formdata.js:

  <script type="text/javascript" src="formdata.js"></script>

use XMLHttpRequest.

  var formData = new FormData();
  formData.append("username", "sam");
  // HTML file input, chosen by user
  formData.append("userfile", document.querySelector("#file").files[0]);
  // JavaScript file-like object
  var content = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
  var blob = new Blob([content], { type: "text/xml"});
  formData.append("webmasterfile", blob);
  var xhr = new XMLHttpRequest();
  xhr.open("POST", "");

  if (formData.polyfill) {
      xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + formData.boundary)
      // formData.toString() returns Promise
      formData.toString().then(function(data){
          xhr.send(data);
      })
  } else {
      // normal way
      xhr.send(formData);
  }

use with fetch.

var postData = function(formData){
    if (formData.polyfill) {
        return formData.toString().then(function(data){
            return fetch("", {
                method: "POST",
                headers: {
                    "Content-Type": "multipart/form-data; boundary=" + formData.boundary
                },
                body: data
            })
        })
    } else {
       return fetch("", {
            method: "POST",
            body: datda
       })
    }
}
postData(formData).then(function(res){
  console.log(res)
});

Examples

See examples in examples directory

Reference

This project was inspired by these good projects

最近版本更新:(数据更新于 1970-01-01 00:00:00)

主题(topics):

formdata, formdata-support, frontend

henryluki/FormData同语言 CoffeeScript最近更新仓库

2016-09-02 09:32:40 chinakids/atom-simplified-chinese-menu

2014-12-05 15:06:20 audiocogs/aurora.js

1970-01-01 00:00:00 shadowsocks/shadowsocks-chromeapp

1970-01-01 00:00:00 daraosn/oculus-drone

1970-01-01 00:00:00 tdreyno/iphone-style-checkboxes

1970-01-01 00:00:00 shadowsocks/shadowsocks-gui