✅ ASP.Net Core API which accepts a pdf file and can return it

Hello guys, I'm new to developing Web APIs with ASP.Net Core. Is there a beginner guide which shows how to implement an API which accepts a PDF that is sent with a HTTP Request Then I do some logic on the PDF and return the modified PDF as HTTP Response with ASP.Net Core Struggling with the returning a pdf and accepting a pdf part Thanks a lot
9 Replies
Henkypenky
Henkypenky2y ago
if you are new, I wouldn't recommend tackling this project, there is a lot of complexity with PDF's if you still want to do it you will need to probably base64 encode the pdf, set a content-type of application/pdf and this is just the beginning so you need a front end to upload the pdf, encode it, send it, decode it, do stuff (which you will probably need a library for and there are so few that are good and free, probably 1 or 2 and they are not easy to work with) and then encode it again, return it and decode it in the front end and serve it pdf's are a pain
Dingdangdongchingchangchong
Thanks a lot, so to summarize the steps: Http Request: base64 encode pdf set contenttype to application/pdf Logic at my controller: Decode blabla Http Response: Repeat steps in Http Request Thanks a lot again for your input, now I have some keywords I can google to improvise. Got a dot net internship but I didn't learn this language nor framework in uni so I struggled a little (we learned Java) I didn't realize that base64 libraries are apparently missing in C#
Jimmacle
Jimmacle2y ago
they aren't? there are helper methods in the Convert class (which is one of the only things Convert should actually be used for) and should you really be base64 encoding a pdf file if you're setting the mime type to application/pdf?
Dingdangdongchingchangchong
Did I understand the first answer wrong? I understood it like base64 encode and then set the mime type But apparently just mime type is enough?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
In case you did not read the docs: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0 Client Code:
private readonly HttpClient _httpClient;

private static async Task UploadSampleFileAsync()
{
await using var stream = File.OpenRead("./Test.txt");
using var request = new HttpRequestMessage(HttpMethod.Post, "/api/whatever/upload");
using var content = new MultipartFormDataContent
{
{ new StreamContent(stream), "file", "Test.txt" }
};

request.Content = content;

await client.SendAsync(request);
}
private readonly HttpClient _httpClient;

private static async Task UploadSampleFileAsync()
{
await using var stream = File.OpenRead("./Test.txt");
using var request = new HttpRequestMessage(HttpMethod.Post, "/api/whatever/upload");
using var content = new MultipartFormDataContent
{
{ new StreamContent(stream), "file", "Test.txt" }
};

request.Content = content;

await client.SendAsync(request);
}
ServerCode:
[ApiController]
[Route("api/whatever")]
public class FileController : ControllerBase
{
[HttpPost("upload")]
public IActionResult Upload([FromForm] IFormFile file)
{
// code responsible for file processing
return Ok();
}
}
[ApiController]
[Route("api/whatever")]
public class FileController : ControllerBase
{
[HttpPost("upload")]
public IActionResult Upload([FromForm] IFormFile file)
{
// code responsible for file processing
return Ok();
}
}
The field's name in the body when doing the POST, needs to match the binded field in the Controler's Action https://media.discordapp.net/attachments/569261465463160900/1001188081069735936/unknown.png
Upload files in ASP.NET Core
How to use model binding and streaming to upload files in ASP.NET Core MVC.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server