0

I'm using PDF.js in my Blazor server application, and it works fine with local files, but now I want to find a way to get pdf files on a network storge to expose them via some endpoint (controller) so that can do a GET request for them. I found some sample code, but most of them are using some content instead of a file path on a storage, like this:

"SaveAsFile" sample code using blazor

function saveAsFile(filename, bytesBase64) {
    var link = document.createElement('a');
    link.download = filename;
    link.href = "data:application/octet-stream;base64," + bytesBase64;
    document.body.appendChild(link); // Needed for Firefox
    link.click();
    document.body.removeChild(link);
}

can anyone help me for changing the usage of a file path instead of bytesbase64 in "saveAsFile" in above example code.

ashokoienia
  • 133
  • 7

1 Answers1

0

I'll start by saying that ASP.NET Core has support for this out of the box without necessitating the use of a controller for this purpose via the UseStaticFiles() method in Startup.cs under ConfigureServices() per the following example:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  app.UseStaticFiles(); //Makes js, css, and images available without authentication
  app.UseRouting();
  app.UseAuthentication();
  app.UseAuthorization();
  app.UseStaticFiles(new StaticFileOptions {
    FileProvider = new PhysicalFileProvider("//server/share/file.pdf"),
    RequestPath = "/pdfs"
  });
}

Now, just make your GET call to ://example.com/pdfs/file.pdf and you'll serve the actual file back to them, complete with authentication support.

Whit Waldo
  • 4,806
  • 4
  • 48
  • 70