简体   繁体   中英

Read zip file from URL to ArrayBuffer with Javascript

I have a front end javascript library into which I'm attempting to load a zip file. The libraries documentation suggests that if I wish to be loading a zip file from a URL (I do) then I need to have the data in an arraybuffer.

As I'm not a Javascript person I'm having some trouble discerning how I might be able to do this.

The code I believe that I ultimately need to use is the following, where I assume buffer is the name of my buffer:

shp(buffer).then(function(geojson){});
//or
shp.parseZip(buffer)->returns zip

So the question is, if I have a file at example.com/myfile.zip how do I use vanilla javascript to get that into an ArrayBuffer? I assume once that is done I'll have what I need to feed into that code snippet above.

Plenty of googleing keeps getting me results that don't seem relevant. The File_API docs by Mozilla don't provide an example that I can see.

You can use fetch .

fetch('example.com/myfile.zip').then(res => res.arrayBuffer()).then(arrayBuffer => {
    // use ArrayBuffer
});

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