Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

reactjs - Creating zip file download from an ascii response fails to open due to unsupported format?

Creating zip file download from an ascii response fails to open due to unsupported format. In postman I can confirm that when making my get request and saving the file for this exact same request the zip behaves correctly. So, I'm assuming that this is an error in my code.

The response includes both a content-type (application/zip) and content-disposition (attachment; filename=exampleFileName.ascii.zip). Here is my attempt:

    .then(async (response) => {
        const convertReq = ['ascii'];
        const sliceFileName = response.headers['content-disposition'].indexOf('=') + 1;
        const fileName = response.headers['content-disposition'].slice(sliceFileName);
        const contentType = response.headers['content-type'];
        let raw;
        if (convertReq.includes(Object.keys(exportFormats)[0])) {
            const buffer = new ArrayBuffer(response.data.length);
            raw = new Uint8Array(buffer);
        }

        const newBlob = new Blob([raw], { type: contentType });
        const url = window.URL.createObjectURL(newBlob);
        const link = document.createElement('a');
        link.href = url;
        link.download = fileName;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);

Any suggestions would be helpful. Thanks in advance

question from:https://stackoverflow.com/questions/65944456/creating-zip-file-download-from-an-ascii-response-fails-to-open-due-to-unsupport

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...