0

I have ASP.NET Core MVC back-end api. One controller returns File from server. Is there a way to make request to api route by [href] attribute of <a> tag? Looks like it tries to call React route but not make a request to server.

Also I made AJAX call to that controller and got back file as a string (screenshot is attached). Why is it a string, shouldn.t it be a byte array? How to build back file from that string? (it's a .pdf file). I have an empty PDF if use JavaScript new File([], 'name', {options}).

ASP.NET Core controller returns PDF this way:

return PhysicalFile(Path.GetFullPath(relativePath), "application/pdf", reportName);

In React I receive it as a string this way:

let stringPDFBinary = await ReportService.getReport(id, reportFileName)

I just need to download file from api by any way. enter image description here

User
  • 96
  • 7
  • 1
    Can you share some of your code? – Gh05d Jun 08 '21 at 11:55
  • Verify me of something. Do you want to download the pdf from your backend API and appear downloaded in your app(browser - lower left corner) correct? i have that code just verify me of that. – Júlio Almeida Jun 08 '21 at 12:01
  • @JúlioAlmeida I just need to download pdf from API. Asp.net core returns it from controller. In react I receive it throufg AJAX call as it is at screenshot. I have no idea how to make it pdf file again in React and download it. – User Jun 08 '21 at 12:06
  • @Gh05d Added code – User Jun 08 '21 at 12:09
  • You can't use AJAX to download file; but you can manually save the generated/ downloaded content (blob). Try using [FileSaver.js](https://github.com/eligrey/FileSaver.js/wiki/Saving-a-remote-file#using-ajax--filesaver)... – IronGeek Jun 08 '21 at 12:36
  • @IronGeek Why can't I use ajax? I did it in angular but can't in react for some reason. – User Jun 08 '21 at 12:43

2 Answers2

0

So, the answer is here: PDF is blank when downloading using javascript

The same problem. Let it be one more topic, easier to find for others. The AJAX response is encoded string. In request config set 'responseType = 'arraybuffer'' somehow and receiving pdf will not be blank. Solved.

User
  • 96
  • 7
0

I Just copied and pasted from the code source. The problem seems to be the same that i had:

Asp net controller:

    [HttpGet]
    [Route("File")]
    [AllowAnonymous]
    public IActionResult GetFile(string key)
    {
        var file = (FileCacheValue)_fileCache.Cache[key.Replace(" ", "+")];

        if (file == null)
            return NotFound();

        Response.Headers["content-disposition"] = $"inline;filename={file.Name}.pdf";

        return File(file.Data, "application/pdf");
    }

In this case comes from a cache system. The data is a byte array.

Front-end React:

const onClick = () => 
{
   window.open(pdfByteArray, '_blank', 'fullscreen=yes');
}

Exactly what i have. I just put the data on a new window and open the pdf.

The Ajax part is straight forward, get the value from the response and set it on a variable

Júlio Almeida
  • 1,539
  • 15
  • 23