JavaScript - How to transform binary pdf to readable pdf

164 Views Asked by At

when I want to open or download a PDF file uploaded to a database in .NET Core, it returns: to visualize it: binary visualization and when I want to download it, the blank sheets appear: Blank PDF when download

Does somebody knows why it could be? Here's my code:

C#

                byte[] bytes = System.IO.File.ReadAllBytes(file.FullName);
                //var filestream = new System.IO.FileStream(file.FullName, System.IO.FileMode.Open);                var resp = new HttpResponseMessage(HttpStatusCode.OK);
                resp.Content = new ByteArrayContent(bytes);
                resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

JS + superagent (superagent should be required)

async visualizeOrDownload(id, type) {
      var request = require("superagent");
      var apiBaseUrl = `/api/anex/doc/${id}`;
      var self = this;
      var req = request.post(apiBaseUrl);


      
      req.end(function (err, res) {
        if (err) {
          console.log("error ocurred");
        } else {
          var blob = new Blob([res.text], {
            type: "application/pdf",
          });
          var element = document.createElement("a");
          document.body.appendChild(element);
          element.download = `${id}.pdf`;
          element.href = window.URL.createObjectURL(blob);
          element.style.display = "";
    
          if (type === "download") {
            element.click();
          } else {
            window.open(element.href);
          }
        }
      });
    }
0

There are 0 best solutions below