简体   繁体   中英

using javascript, angular.js, and sql.js, how do I get a file object from a url?

I'm getting the URL to a sqlite db file on my server from the browser url using query string.

So in the app I have the file url.

Then I'm calling an angular $http.get function on that url.

This gives me the data in the file, but the library I'm using to read the data requires a file object.

Is there a way to create a file object from the paramiters inside $http.get? The parameters are data, status, headers, and config, I also have the file name.

I've tried constructing a blob but I keep getting an error saying "the database disk image is malformed." Here is the blob construction Inside the $http.get function:

            ...
            var blob = [];
            var blob2 = {fileName:null};
            $http.get(fileName).
              success(function (data, status, headers, config) {
                blob[0] = data;

                var theBlob = new Blob(blob, {name:fileName},{status:status},{headers:headers},{config:config}, {type : ''});
                var r = new FileReader();
                r.onload = function () {                    
                    var Uints = new Uint8Array(r.result);
                    db = new SQL.Database(Uints); 
                ...

I've also tried building the blob by adding the $http.get data, status, headers, and config parts as more parts of the blob[] array, and passing that to the new Blob constructor...

Any help would be really appreciated, thanks!

I ended up using a button and assigning a load from url event to it, this way I didn't have to build my own blob. When i built my own blob, sql.js was unable to parse my sqlite db file. It seemed as if the headers were missing, but I couldn't get them added in a way that sql.js would read correctly.

With a modified use case, the load from url button option works fine. I even have the button hidden unless there is data returning from the query string in the url. This means the button to load from url only shows up when the url has a query string in it.

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