1

In mobile apps, we usually have to convert a file/image to base64 and then send generated string via API to the server and re-create a file/image.

However in web apps, we usually see below tag to handle file/image uploads.

<input type="file"> 

My question is does server side languages like PHP (i.e. move_uploaded_file) or .NET automatically follow the same conversion behind the scenes as above (convert/encode a file/image to base64 string -> decode and recreate file on server) or there is some different mechanism

Ayub
  • 510
  • 2
  • 6
  • 21
  • 1
    `In mobile apps, we usually have to convert a file/image to base64 ` No. I disagree. That would cause for 30% more bytes to upload. We dont do such things. – blackapps Aug 05 '22 at 08:11
  • 1
    The browser will not encode the file base64 to begin with. So on server side there is nothing to do. Your question should have been for what the uploading browser does. – blackapps Aug 05 '22 at 08:14
  • I agree, there's generally no reason to base64-encode the file when uploading it, you can upload the raw file. And browsers and other languages don't do it automatically, no. – ADyson Aug 05 '22 at 08:36
  • @blackapps in android for instance if one needs to upload an image file to server, how do you recommend going about it – Ayub Aug 05 '22 at 10:28
  • @ADyson if possible can you please direct me towards what happens when we select a file and click "upload" button (on a web page), how that transfer is performed by browsers/server side languages – Ayub Aug 05 '22 at 10:33
  • 1
    A browser sends a HTTP multipart/form-data request, with any form fields in one part of the request and the binary data of the file (or files) in a separate part (or parts). Server side languages can generate any type of HTTP request you want (just like your android code) so there's no fixed definition for that. It _could_ send a conventional multipart request, or it could encode the file as base64 like you're doing, or even just a raw byte stream in the body of the request. Depends what's required by the receiving server. There's no default behaviour there, it depends what code you write. – ADyson Aug 05 '22 at 10:57
  • All depends on what the server is expecting. If the server expects base64 encoded files we use base64 encoding. If the server just expects a file then we upload the bytes of the file. – blackapps Aug 05 '22 at 10:58
  • 1
    See also the links at https://stackoverflow.com/a/19712083/5947043 . I'd expect the HTTP client libraries in Android can cope with making one quite well without you having to literally code the raw HTTP markup. – ADyson Aug 05 '22 at 11:00
  • @ADyson Thanks for explaining, so multi-part request is combination of text + files (images, music, documents converted to binary and not base64), however we can modify via code however a server accepts data i.e. binary, base64 or maybe a new mechanism – Ayub Aug 11 '22 at 16:32
  • 1
    Exactly yes, that's right – ADyson Aug 12 '22 at 05:47

0 Answers0