简体   繁体   中英

How to use a Model with octet-stream data in requests?

Give a HTTP Filesystem API which accepts

GET /storage/filename
PUT /storage/filename
Request body: the file contents

and these headers

Content-Length: ...
Content-Type: application/octet-stream

How can one build a backbone.js File Model which works with it? (eg get file contents in a variable, put back updated contents)

If backbone.js doesn't support this, how would jQuery ajax requests look like?

While I haven't found support in jQuery/backbone, here's how it can be done:

var xhr = new XMLHttpRequest();
xhr.open('GET', "url", true);
xhr.setRequestHeader("Authorization", token);
xhr.responseType = "text";

xhr.onload = function(e) {
  if (this.status == 200) {
    var contents = this.response;
    console.log("RECEIVED " + contents);
   }
};

xhr.send();

More details at http://www.html5rocks.com/en/tutorials/file/xhr2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM