Binary file access

Ask, and ye shall receive.
thegecko
Posts: 6
Joined: 14:51, 24 Feb 2015

Binary file access

Postby thegecko » 15:41, 24 Feb 2015

Hey team,

it seems the evothings client doesn't include the file or fileTransfer cordova plugins.

How would you recommend loading a binary file (from a URL or the device file system) within evothings?

Kind regards,

Rob

thegecko
Posts: 6
Joined: 14:51, 24 Feb 2015

Re: Binary file access

Postby thegecko » 11:45, 26 Feb 2015

I've got round this by using a $.ajax call along with this awesome jQuery extension to support the 'blob' dataType. I can then download binary files from the interwebs (considering CORS of course) and load the resulting binary into an ArrayBuffer...

// jQuery extension to support blob downlaods
$.ajaxTransport("+*", function(options, originalOptions, jqXHR){
if (options.dataType && options.dataType == "blob") {
return {
send: function(headers, completeCallback){
var xhr = new XMLHttpRequest();
var url = options.url || window.location.href;
var type = options.type || "GET";
var dataType = options.dataType;
var data = options.data || null;
var async = options.async || true;

xhr.addEventListener("load", function() {
var response = {};
response[dataType] = xhr.response;
completeCallback(xhr.status, xhr.statusText, response, xhr.getAllResponseHeaders());
});

xhr.open(type, url, async);
for (var header in headers) {
xhr.setRequestHeader(header, headers[header]);
}

xhr.responseType = dataType;
xhr.send(data);
},
abort: function(){
jqXHR.abort();
}
};
}
});


Return to “Questions and answers”

Who is online

Users browsing this forum: No registered users and 51 guests