尝试捕获块Axios响应类型是Blob上没有错误消息

发布于 2025-01-30 03:24:39 字数 986 浏览 3 评论 0原文

获取错误的错误消息

console.log(error)

devtools网络响应

const handleDownloadPicture = async (ids: number) => {
    try {
        const response = await axios
            .post(`/api/folders/${folderDetails.id}/download`, {
                    resources: ids,
                },{responseType:'blob'},
            )
            .then(({ data: blob }) => {
                const link = document.createElement('a');
                const url = URL.createObjectURL(blob);
                link.href = url;
                link.download = 'myfile.zip';
                link.click();
            });
    } catch (error) {
        console.error(error);

    }
};

我在console.log(error)上收到错误的错误消息

尝试将BLOB响应转换为JSON的多种解决方案,但没有结果

Getting wrong error message

console.log(error)

devtools network Response

const handleDownloadPicture = async (ids: number) => {
    try {
        const response = await axios
            .post(`/api/folders/${folderDetails.id}/download`, {
                    resources: ids,
                },{responseType:'blob'},
            )
            .then(({ data: blob }) => {
                const link = document.createElement('a');
                const url = URL.createObjectURL(blob);
                link.href = url;
                link.download = 'myfile.zip';
                link.click();
            });
    } catch (error) {
        console.error(error);

    }
};

I'm getting the wrong error message on console.log(error)

have tried multiple solutions of converting blob response to json but no result

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2025-02-06 03:24:39

becasue axios.post()是返回承诺,您应该以两种方式使用响应或捕获错误:const wersys =等待Axios.post()或<代码> axios.post()。然后(响应).catch()。

您只能以两种方式使用一次:

案例1:

    try {
       const response = await axios.post(`/api/folders/${folderDetails.id}/download`)
       ....
      } catch(error){
      console.log(error) // <=== Error will be catch here!
      }

案例2:

axios.post(`/api/folders/${folderDetails.id}/download`, {
     resources: ids,
   },{responseType:'blob'},
   ).then(({ data: blob }) => {
       const link = document.createElement('a');
       const url = URL.createObjectURL(blob);
       link.href = url;
       link.download = 'myfile.zip';
       link.click();
     }).catch((error) => console.log(error) //<=== Error will be catch here!

Becasue axios.post() is return Promise, you should use once in two way to get response or catch error: const response = await axios.post() or axios.post().then(response).catch().

You should use only once in two way:

Case 1:

    try {
       const response = await axios.post(`/api/folders/${folderDetails.id}/download`)
       ....
      } catch(error){
      console.log(error) // <=== Error will be catch here!
      }

Case 2:

axios.post(`/api/folders/${folderDetails.id}/download`, {
     resources: ids,
   },{responseType:'blob'},
   ).then(({ data: blob }) => {
       const link = document.createElement('a');
       const url = URL.createObjectURL(blob);
       link.href = url;
       link.download = 'myfile.zip';
       link.click();
     }).catch((error) => console.log(error) //<=== Error will be catch here!
九命猫 2025-02-06 03:24:39
axios.post(`/api/folders/${folderDetails.id}/download`, {
    resources: ids,
  },{
    responseType:'blob'
  }).then(({ data: blob }) => {
    const link = document.createElement('a');
    const url = URL.createObjectURL(blob);
    link.href = url;
    link.download = 'myfile.zip';
    link.click();
  }).catch(async (error) => {
    console.log(JSON.parse(await error.response.data.text()));
  });

axios.post(`/api/folders/${folderDetails.id}/download`, {
    resources: ids,
  },{
    responseType:'blob'
  }).then(({ data: blob }) => {
    const link = document.createElement('a');
    const url = URL.createObjectURL(blob);
    link.href = url;
    link.download = 'myfile.zip';
    link.click();
  }).catch(async (error) => {
    console.log(JSON.parse(await error.response.data.text()));
  });

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文