Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upload image processing using gin #107

Closed
badoet opened this issue Aug 31, 2014 · 13 comments
Closed

upload image processing using gin #107

badoet opened this issue Aug 31, 2014 · 13 comments

Comments

@badoet
Copy link

badoet commented Aug 31, 2014

do gin have a feature to handle multipart form post upload?

@manucorporat
Copy link
Contributor

no, but the http package include an powerful API to handle multipart requests.
http://sanatgersappa.blogspot.com.es/2013/03/handling-multiple-file-uploads-in-go.html

Do you think we should add a higher API in gin.Content?
like: c.Multipart(....)

@johndeng
Copy link

@manucorporat Add this feature is cool. but is better to be a middleware.

@manucorporat
Copy link
Contributor

@johndeng I agree

@johndeng
Copy link

@manucorporat Thanks.

@techjanitor
Copy link
Contributor

How are you supposed to handle file uploads in Gin, then? I can't seem to access FormFile from Context:
c.Request.FormFile("file")

[GIN] 2014/09/01 - 03:48:13 | 400 | 248.788us | 172.4.231.9 POST /post
Error #1: unknown content-type: multipart/form-data
Meta: Operation aborted

@techjanitor
Copy link
Contributor

Oh nevermind it was an issue with binding. If it helps the OP this is how you handle multipart forms:

c.Request.FormValue("value")
file, fileheader, _ := c.Request.FormFile("file")

@badoet
Copy link
Author

badoet commented Sep 2, 2014

Hmm, i realize that there are many ways to do this.
in my use case, i will need to resize the uploaded image into multiple sizes.
so i cant use the more optimized Buffering method.

// the not so optimized way.
file, _, err := r.FormFile("file")
img, _, err := image.Decode(file)
conf := resize.Resize(55, 0, img, resize.NearestNeighbor)
out, err := os.Create("public" + imgPath)
err = jpeg.Encode(out, conf, nil)

another option is to perform the basic upload image using the optimized buffer method
and then read the uploaded image from the server disk to perform further resizing work if needed..
i.e. can provide image resizing as optional additional function..

@phrozen
Copy link

phrozen commented Sep 29, 2014

I don't think Middleware is the best option for this kind of stuff, sometimes you want to process the data or save the file, etc... but files sometimes are optional, its too broad of a use case.

What would be pretty useful is a simple function within Gin that takes the current context and saves the file to disk given a route (and handles all temp file and copy for the user). You can perform validations and such before, and just save it if needed.

@whidbey
Copy link

whidbey commented Apr 26, 2015

dont know what's the plan for the image upload .i am trying to do as above and use c.Request.Formfile("image") ,which may not so convinence.

@nazwa
Copy link

nazwa commented Apr 26, 2015

I use the following stuff, just like @badoet suggested and it works super fine.

file, _, err := c.Request.FormFile("file")
x, _, err := image.Decode(file)
// Do something with the image + error handling for above 2 
jpeg.Encode(f, file, &settings)

@whidbey
Copy link

whidbey commented Apr 26, 2015

@nazwa and your settings detail?

@nazwa
Copy link

nazwa commented Apr 26, 2015

Anything you want really. In my case it's

settings := jpeg.Options{Quality: 80}

@whidbey
Copy link

whidbey commented Apr 28, 2015

@nazwa thankyou

@badoet badoet closed this as completed Nov 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants