I’m trying to read a simple sqlite database. as described here I need to read a local database file, convert it to Uint8Array and read as readAsArrayBuffer format.
suggested method is XMLHttpRequest as described here. it perfectly worked but it dosent work in chrome browser.
var xhr = new XMLHttpRequest();
xhr.open('GET', 'DataBaseBackup.db', true);//DataBaseBackup.db is a local file
xhr.responseType = 'arraybuffer';
xhr.onload = function (e) {
var uInt8Array = new Uint8Array(this.response);
var db = new SQL.Database(uInt8Array);
var contents = db.exec("SELECT * FROM contacts");
console.log(contents);
// contents is now [{columns:['col1','col2',...], values:[[first row], [second row], ...]}]
};
xhr.send();
how can i read this database file locally?
thanks
Related
I am trying to download a file in a service worker. The file came from a blob.
I already know that i can download a blob by doing this
var urlCreator = window.URL || window.webkitURL;
var blobURL = urlCreator.createObjectURL(blob, { type: "octet/stream" });
or by using something like FileSaver.js
But today, i am trying to "render" the bytes array of the blob in the page and force the browser to download the document.
Why ?
I am trying to do that because, some browser that i am trying to reach do not support url like "blob:https:......"
So i try to find a way to render the bytearrays of my blob directly in javascript.
I know that i can push headers with worker, but not sure that is the right strategie.
I also try something like that
var xhr = new XMLHttpRequest();
xhr.open('GET', obj.URL, false);
xhr.overrideMimeType("application/octet-stream");
//xhr.setRequestHeader("Accept", "application/octet-stream");
xhr.responseType = "arraybuffer";
xhr.onload = function (e) {
self.Response(xhr.response);
//if (this.status == 200) {
// var myBlob = this.response;
// // myBlob is now the blob that the object URL pointed to.
//}
};
xhr.send();
In my worker.js
ps: Please avoid to told me, send you base64 blob content to your server and send back the file to the browser, i would like to find a pure javascript solution.
Is it possible ?
I am using WebAudioRecorder.js for making online recordings in an R Shiny app, see:
https://github.com/addpipe/simple-web-audio-recorder-demo
As a format, I chose the wave format, and in the JavaScript code, the recording is obtained as a blob. I would like the program to save this blob on the server without any dialog.
Here, you shouldn't set the hole filePath in javascript, you should give it a filename and then php should put it in the correct folder.
function uploadWaveBlob (blob, encoding) {
var xhr = new XMLHttpRequest();
var formData = new FormData();
var fileName = Date().toISOString() + '.' + encoding;
formData.append("Wav", blob, fileName);
xhr.open('POST', uploadUrl);
xhr.onload = function () {
console.log('xhr complete');
};
xhr.send(formData);
}
imagine if i would upload something to like /etc/hosts or something
The following site gives code that shows how to upload a blob to the server:
https://gist.github.com/primaryobjects/d6cdf5d31242a629b0e4cda1bfc4bff9
The complete solution is available at:
https://github.com/heeringa0/simple-web-audio-recorder
and shows how to integrate the Simple WebAudioRecorder.js in an R Shiny app where the recording is saved to the server.
Supposing I want to upload a stream of random data from my browser to a remote server. I would use something like
var oReq = new XMLHttpRequest();
oReq.open("POST", url, true);
oReq.onload = function (oEvent) {
// Uploaded.
};
var blob = new Blob(['abc123'], {type: 'text/plain'});
oReq.send(blob);
and repeat this as many times as I have chunks to upload.
But, what if I wanted to send it using a single XHR request ? Is it possible to override the Blob function and do something like
function Blob(arr, options) {
var chunkSize = 1024;
MyBlob.prototype.nextChunk(function () {
return arr.splice(0, chunkSize);
});
}
I know that nextChunk does not exist, obviously, but maybe there is some interface that can be overridden ? I looked at the Blob specification and I tried using an alternative Blob implementation (that I modified slightly to force overriding window.Blob), but nothing was sent to the server.
Is it possible to generate a file with javascript and send it to server? I have a section in my web page where the user can modify the style of its page, and I want to generate the css file with that style configuration before to send it to the server. Is this possible? Thanks!
var blob = new Blob(["css content"],{type:"text/css"});
var formData = new FormData()
formData.append("file",blob,"fileName");
var xhr = new XMLHttpRequest();
xhr.open('POST', '/server', true);
xhr.onload = function(e) { /**get answer from server*/};
xhr.send(formData);
I want to create a Chrome Application. If I put a JSON file in the same folder how do I access the file? I want to find the correct way of getting the text into a variable, something like the example I found:
var json = $.getJSON("data.json");
I could not find in this example where the $ object is defined. It must be some class. How can I use this in a Chrome Application?
I found out that it works the exact same way as if it was cross-origin:
var xhr = new XMLHttpRequest();
xhr.onload = function() {
console.log(JSON.parse(this.response));
};
xhr.open('GET', '/data.json', true);
xhr.send();
Read more at https://developer.chrome.com/apps/app_external
For those who are developing a Chrome Extension, you need to do it a little different according to this site: https://developer.chrome.com/extensions/xhr (I haven't tested this.);
var xhr = new XMLHttpRequest();
xhr.onload = function() {
console.log(JSON.parse(this.response));
};
xhr.open('GET', chrome.extension.getURL('/data.json'), true);
xhr.send();
Edit
If you want to use jQuery (as in the example that you found.):
$.getJSON('data.json', function(data) {
console.log(data);
});