Smart Actions Download Csv File

Hello,
I have created a new smart action which has only one work to download a csv file.

I am getting the blob object but not able to find how to download that.

Please help out.

Hello @Kashish_Maggu,

Have you set the option download: true in your smart action? This should trigger a download in your browser :slight_smile:

yes i have set that already

Hello @Kashish_Maggu,

Could you mind sharing your smart action code?
It’ll be easier in order to assist you :pray:

Also, a few threads might help you debug this as well

Thanks in advance

router.post('/actions/download-in-app-properties', permissionMiddlewareCreator.smartAction(),
  (req, res) => {
    fetch(api_url, {
      method: 'GET',
    })
      .then(response => response.blob())
      .then(data => {
        res.sendFile(data, (error) => {
          if (error) {
            console.log("error", error)
          }
        });
      })
      .catch((error) => {
        res.status(400).send(error);
      });

  })
.then(data => {
        res.send(data);
      })

tried this snippet from one of above tickets that you shared but it is downloading empty json file. the file that i am downloading is csv as well

tried this snippet from one of above tickets that you shared but it is downloading empty json fil

Did you change the header according to the fact that you are using CSV instead of JSON?
You may also need to convert this blob to either a text or a buffer.

Since I don’t know the kind if response sent by your fetch(api_url), I’ll not be able to provide any code in order to help :confused:

Let me know.

the api is returning the csv file.
also what will be the headers that i should use?

Most likely something like

    const filename = `${params.filename}.csv`;
    response.setHeader('Content-Type', 'text/csv; charset=utf-8');
    response.setHeader('Content-disposition', `attachment; filename=${filename}`);

tried this but getting the file data like ‘[object Response].csv’ instead of csv values

router.post(’/actions/download-in-app-properties’, permissionMiddlewareCreator.smartAction(),
(req, res) => {
fetch(process.env.BASE_HOST_API + ‘/api/v1/user/config/get-current-in-app-properties/’, {
method: ‘GET’,
headers: {
“authorization”: "Bearer " + process.env.BEARER_AUTH_TOKEN,
}
})
.then(data => {
const filename = ${data}.csv;
res.setHeader(‘Content-Type’, ‘text/csv; charset=utf-8’);
res.setHeader(‘Content-disposition’, attachment; filename=${filename});
res.sendFile(filename)
})
.catch((error) => {
res.status(400).send(error);
});

})

You should console.log(data) in your code, and make sure you able to convert it to text. Depending on the response of the service, data.text() might be enough in your case. But as stated, since I do not have access to the service, that’s the best I can do.

Once you are able to receive your csv as text from the service, you should be able to use the provided header and send the file directly.

thanks it worked, data.text() was missing and earlier i was using blob()

also is there any way to show a custom pop up that might contain response from api

Not sure what you mean by “also is there any way to show a custom pop up that might contain response from api”. I would suggest to open a new thread and elaborate a bit around your need.

I’m marking this thread as resolve since the initial topic seems to work for you now.