Answer a question

I am trying download a zip file from nodejs using vuejs.

My problem is that I have a weird underscore around the fileName when the dialog appears.

If I set manually:

const fileName="xmlFile.zip";

I have no problem.

I attached an image of the problem.

Here is the headers returnning from node:

Accept-Ranges: bytes
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Disposition
Cache-Control: public, max-age=0
Connection: keep-alive
Content-Disposition: attachment; filename="xmlFile.zip"
Content-Length: 164
Content-Type: application/zip
Date: Sun, 19 Jul 2020 13:55:15 GMT
ETag: W/"a4-17367574de4"
Last-Modified: Sun, 19 Jul 2020 13:50:41 GMT
X-Powered-By: Express

enter image description here

What am I doing wrong?

 //vuejs front end
        let response = await axios.post('/generateLoteXMLZIPConsulta');;
        let blob = await new Blob([response.data], {
          type: "application/zip",
        });
        const link = document.createElement("a");
        link.style.display = "none";
        link.href = window.URL.createObjectURL(blob);
        const fileName = response.headers["content-disposition"].match(
          /filename=(.*)/
        )[1];
        link.download = fileName;
        link.click();
        window.URL.revokeObjectURL(link.href);

 //backend nodejs
 router.post("/generateLoteXMLZIPConsulta", async (req, res) => {
    ....
    ....
    res.download(
            path.resolve(__dirname, "../../file.zip"),
            "xmlFile.zip"
    );
})      

Answers

After reviewing your additional details, I think the underscores are added because of your regex, that captures also double quotes it seems.

Based on your details, response header content is:

Content-Disposition: attachment; filename="xmlFile.zip"

And then you extract it like this:

const fileName = response.headers["content-disposition"].match(
          /filename=(.*)/
        )[1];

Try logging into console fileName. I think it would require stripping double quotes.

Logo

前往低代码交流专区

更多推荐