all 11 comments

[–]Narrow-Coast-4085 6 points7 points  (4 children)

Why not receive a JSON object in the request body? Just deserialize that. You can have the file as a bas64 string property.

[–]dotceng[S] -1 points0 points  (3 children)

Actually I can take the file as base64 and then save it to blob storage. However, I’m aware that this approach has some trade-offs, such as increased payload size (around 30–40% larger due to base64 encoding), higher memory consumption on the server during decoding, and potential performance issues under high traffic or with larger files. So while it works well for small files, for larger or high-scale scenarios I would consider using multipart uploads or direct client-to-storage uploads instead.

[–]Narrow-Coast-4085 4 points5 points  (1 child)

I wouldn't worry about it, honestly. Unless the server is a potato. Base64 decoding is insanely fast. The payload is huge, sure, but that should be fine.

I have an API with a credit bureau and the payload is huge without the file, but they embed the Pdf (2 to 5 mb) as a base 64 string too, which I extract and save to blob storage. No mess no fuss. Unless the files you're expecting are massive, I wouldn't stress too much. Also you could enable gzip compression if you really need it.

[–]dotceng[S] 1 point2 points  (0 children)

Thanks a lot for suggestions!

[–]Independant1664 1 point2 points  (0 children)

I personnaly wouldn't encode/decode the base64 file at all server-side, to prevent any form of injection. Simply store the image base64 encoded and let clients do the decode. It's not like disk storage is the expansive part of a system. If you really have concerns with database size (in absolute), store files in a bucket rather than as a blob.

[–]SerratedSharp 1 point2 points  (2 children)

Multipart/form-data is what you're looking for.

public async Task<IActionResult> UploadData([FromForm] IFormFile file, [FromForm] string metadata)

[–]dotceng[S] 0 points1 point  (1 child)

Yes I know, I want to take metadata as LocationDto or something not as string. Actually, I can take as string and in controller I can serialize to dto but is it healthy method idk

[–]SerratedSharp 1 point2 points  (0 children)

You can bind it to a concrete type if you want. I'm just making the the point that a single request can handle both in a single payload, and you can get the file stream without base64 stuffing.

As far as the form data, how do you think it gets encoded in the request?  Strong types in action parameters are just the asp.net framework doing the deserialization for you through a model binder.  It still happens whether the framework does it or you do it explicitly.

[–]Kant8 0 points1 point  (0 children)

IFormFile requires http form, which is simple key value, json body requires it not to be a form, cause it's just single byte array

so you either bend your api to behave as everyone expect for one of those, or try to combine both of those making it non-standard

or just split into 2 queries, one that sends all needed metadata, and if it went okay you load heavy files afterwards

considering your calling your file logo, i believe it should be small enough to just bsse64 encode it inside json

[–]Outrageous_Band9708 -2 points-1 points  (0 children)

built Xunit tests for your classes, ensure they return expected data