title: JavaScript - How to transform binary pdf to readable pdf
body: <p>when I want to open or download a PDF file uploaded to a database in .NET Core, it returns: to visualize it:
  <a href="https://i.stack.imgur.com/Gxcyl.png" rel="nofollow noreferrer">binary visualization</a>
  and when I want to download it, the blank sheets appear:
  <a href="https://i.stack.imgur.com/TVUHo.png" rel="nofollow noreferrer">Blank PDF when download</a></p>
  <p>Does somebody knows why it could be? Here's my code:</p>
  <p><strong>C#</strong></p>
  <pre><code>                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(&quot;application/pdf&quot;);
  </code></pre>
  <p><strong>JS + superagent (superagent should be required)</strong></p>
  <pre><code>async visualizeOrDownload(id, type) {
        var request = require(&quot;superagent&quot;);
        var apiBaseUrl = `/api/anex/doc/${id}`;
        var self = this;
        var req = request.post(apiBaseUrl);
  
  
        
        req.end(function (err, res) {
          if (err) {
            console.log(&quot;error ocurred&quot;);
          } else {
            var blob = new Blob([res.text], {
              type: &quot;application/pdf&quot;,
            });
            var element = document.createElement(&quot;a&quot;);
            document.body.appendChild(element);
            element.download = `${id}.pdf`;
            element.href = window.URL.createObjectURL(blob);
            element.style.display = &quot;&quot;;
      
            if (type === &quot;download&quot;) {
              element.click();
            } else {
              window.open(element.href);
            }
          }
        });
      }
  </code></pre>