hi
I’m trying to download a video using xhr and then write it to the local file system using cep.fs.writeFile() from this API:
The following codes loads the video and shows it in a <video> tag. But writing it causes error 2, unknown error. Probably because I don’t pass the data correctly.
And is there a way to use constants to create commonly used paths that are valid across os platforms, like in AIR for example File.documentsDirectory? Probably not because usually js cannot access the local file system? How should I for instance set the path to a folder named ’blah’ in my Documents folder, on mac and windows?
And how should I pass the data in the writeFile method in example below, this.response?
And should I base64 encode the thing?
Btw, I can comfortably create, write and delete text files with the same code and api, changing the path to something like:
'/Users/my_user_name/Documents/test/test.txt';//MAC
I just wonder how to do this with mp4 and mov files.
var downloadFile = function (url) {
’use strict’;
window.URL = window.URL || window.webkitURL;
var xhr = new XMLHttpRequest();
xhr.open(’GET’, url, true);
xhr.responseType = ’blob’;
xhr.onload = function(e) {
var video = document.createElement(’video’);
video.src = window.URL.createObjectURL(this.response);
video.autoplay = true;
document.body.appendChild(video);
};
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
console.log(’onreadystatechange’, xhr.status);//200
var path = '/Users/my_user_name/Documents/test/test.mp4';//or use some OS CONSTANT?
var result = window.cep.fs.writeFile(path, this.response, cep.encoding.Base64);
if (0 == result.err) {
console.log(’write succes’, result.data);
}
else {
console.log(’write error: ’, result.err);
}
}
};
xhr.send();
};
downloadFile(’http://someurl.com/my.mp4’);
Thanks!