Javascript 如何使用PostAPI从reactjs下载ZIP文件?
如何使用PostAPI从reactjs下载zip文件。 请求以二进制形式来自nodejs您可以像 如果你用这种方法取Javascript 如何使用PostAPI从reactjs下载ZIP文件?,javascript,reactjs,Javascript,Reactjs,如何使用PostAPI从reactjs下载zip文件。 请求以二进制形式来自nodejs您可以像 如果你用这种方法取 fetch('URL', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify({ //Body }) }).then((response)=>{
fetch('URL', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
//Body
})
}).then((response)=>{
//here is youu want zip data
var zip = new JSZip();
var zipData = response.data //
// Add an top-level, arbitrary text file with contents
zip.file("response.txt", zipData);
// Generate the zip file asynchronously
zip.generateAsync({type:"blob"})
.then(function(content) {
// Force down of the Zip file
saveAs(content, "zipFile.zip");
});
}).catch((error)=>{
console.log(error)
})
您可以在客户端使用JsZip。
然后,使用axios执行请求。这样地:
请求=(当前URL:string):承诺=>axios({
url:currentUrl,
方法:“GET”,
responseType:'blob',
})。然后((响应)=>{
constURL:string=window.url.createObjectURL(新Blob([response.data]);
});
欢迎来到SO;)请在网上阅读这篇文章。这将包括对您试图实现的目标、代码(或相关代码段)的正确描述,以及您迄今为止所做的努力,以及可能的错误消息。还建议提供完整的api。api是post。那么我如何下载它api是post。那个么我怎样才能下载它呢?我完全忽略了这一点。但当您实际上从服务器获取数据时,为什么要使用POST Api呢?我认为这不是调用Api的正确方法,但情况是。。在文章正文中,我们以数组形式提供文件路径,该文件将被压缩。
fetch('URL', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
//Body
})
}).then((response)=>{
//here is youu want zip data
var zip = new JSZip();
var zipData = response.data //
// Add an top-level, arbitrary text file with contents
zip.file("response.txt", zipData);
// Generate the zip file asynchronously
zip.generateAsync({type:"blob"})
.then(function(content) {
// Force down of the Zip file
saveAs(content, "zipFile.zip");
});
}).catch((error)=>{
console.log(error)
})
You can use JsZip on Client Side.
Then, do a request with axios. Like this:
request = (currentUrl: string): Promise<void> => axios({
url: currentUrl,
method: 'GET',
responseType: 'blob',
}).then((response) => {
const url: string = window.URL.createObjectURL(new Blob([response.data]));
});