Downloading a PDF file from an Endpoint #1101
nick-goloborodko
started this conversation in
General
Replies: 2 comments
-
any update on this ? did you manage to solve it ? when using "new FileContentResult(data.Data, data.MimeType);" I'm getting a json response with fileContents encoded in base64, which previously I used to get a download button from swagger and used to receive the file as binary |
Beta Was this translation helpful? Give feedback.
0 replies
-
You can try: public async Task<IResult> ReadAndReturnFile()
{
// Get the full path to the file in the root directory
var fullPath = Path.Combine(AppContext.BaseDirectory, "sample.pdf");
if (!File.Exists(fullPath))
{
return Results.NotFound("File not found.");
}
// Read file contents
var fileBytes = File.ReadAllBytes(fullPath);
// Return the file as a response
return Results.File(fileBytes, "application/pdf", Path.GetFileName(fullPath));
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This is driving me mad. Seems like such a simple task - but I just can't seem to figure out how to accomplish with .NET 8 CA solution.
I have a PDF file that sits on a protected cloud storage. I need to develop a method that takes that and returns the file contents with mime type . The issues with the code below is that it returns data as JSON, with file contents base64 encoded, rather than the http response itself being of the correct mime type and just the file contents in the body of the response?
I used to be able to do something like this in MVC controllers, but this approach no longer appears to work?
Any help will be greatly appreciated!
Beta Was this translation helpful? Give feedback.
All reactions