Search code examples
file-uploadgo

golang - How to check multipart.File information


When a user uploads a file using r.FormFile("file") you get a multipart.File, a multipart.FileHeader and an error.

My question is how to just obtain information about the uploaded file . For example, its size, its dimensions if it's an image, and so on and so forth.

I have literally got no idea on where to start so any help would be great.


Solution

  • The file name and MIME type can be obtained from the returned multipart.FileHeader.

    Most further meta-data will depend on the file type. If it's an image, you should be able to use the DecodeConfig functions in the standard library, for PNG, JPEG and GIF, to obtain the dimensions (and color model).

    There are many Go libraries available for other file types as well, which will have similar functions.

    EDIT: There's a good example on the golang-nuts mail group.