arrays - 如何在reactjs的新选项卡中打开pdf?

标签 arrays reactjs asp.net-web-api react-redux

We have the pdf document save in data base , from webapi I am returning as byte array,Now from UI I have to open the pdf in new tab of browser, and user should able to download the pdf. How can I achieve this requirement of opening and downloading the pdf document?

最佳答案

以下代码对我有用

try {
    await axios
      .get(uri.ApiBaseUrl + "AccountReport/GetTrialBalancePdfReport", {
        responseType: "blob",
        params: {
          ...searchObject,
        },
      })
      .then((response) => {
        //Create a Blob from the PDF Stream
        const file = new Blob([response.data], { type: "application/pdf" });
        //Build a URL from the file
        const fileURL = URL.createObjectURL(file);
        //Open the URL on new Window
         const pdfWindow = window.open();
         pdfWindow.location.href = fileURL;            
      })
      .catch((error) => {
        console.log(error);
      });
  } catch (error) {
    return { error };
  }

关于arrays - 如何在reactjs的新选项卡中打开pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41561884/

相关文章:

javascript - Express 不为 React 重定向

asp.net-mvc-4 - 你如何从 ApiController 访问 HttpContext.Items

serialization - JSON.NET 序列化失败

css - 使用 React 映射图像数组时如何创建图像背景渐变

javascript - 如何在 <Image> 上呈现复选框(React Native)

asp.net - 尝试通过 Web API 请求检索 Azure blob 时出现 403 错误

java - 在 for 循环中绘制图像 x 次

PHP 在类数组中使用类变量

Python 3.x : Print specific array items

jquery如何填充数组