sharmapn
sharmapn

Reputation: 21

Arraybuffer conversion error while Unzipping and Load shapefile with SHP.JS

I am trying to unzip a zipped file, and if one of the files is a shapefile, then load it as a variable. However, from the JSzip docs, I gather that the shp() function accepts a buffer. I am trying to convert to a buffer, but it not working.

console.log("Unzipping now: "); 
var jsZip = new JSZip();
var fileNum =0;
jsZip.loadAsync(v_objFile).then(function (zip) {
    Object.keys(zip.files).forEach(function (filename){
        //now we iterate over each zipped file 
        zip.files[filename].async('string').then(function (fileData){
            console.log("\t filename: " + filename);                             
                //if we found the shapefile file                 
            if (filename.endsWith('.zip') == true){                                         
                zip.file(filename).async('blob').then( (blob) => { 
                    console.log("Downloading File")                           
                    //saveAs(blob, filename);  
                    
                    //const buf = blob.arrayBuffer();
                    const buffer = new Response(blob).arrayBuffer();

                    shp(buffer).then(function (geojson) {
                        console.log(" Loaded");                                                      
                        // THIS CODE IS NOT REACHED
                    });
                });
               console.log("Called loadShapeFile")                 
            }                
        })           
    })
}).catch(err => window.alert(err))

I tried the attached code, but it did not work. The code did not reach the place where it says, "THIS CODE IS NOT REACHED"

Upvotes: 0

Views: 360

Answers (1)

sharmapn
sharmapn

Reputation: 21

This is the code I found as to how to convert from blob to Arraybuffer.

(async () => {
    const blob = new Blob(['hello']);
    const buf = await blob.arrayBuffer();
    console.log( buf.byteLength ); // 5
})();

Upvotes: 0

Related Questions