Giter VIP home page Giter VIP logo

Comments (13)

manucorporat avatar manucorporat commented on April 20, 2024

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(....)

from gin.

johndeng avatar johndeng commented on April 20, 2024

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

from gin.

manucorporat avatar manucorporat commented on April 20, 2024

@johndeng I agree

from gin.

johndeng avatar johndeng commented on April 20, 2024

@manucorporat Thanks.

from gin.

techjanitor avatar techjanitor commented on April 20, 2024

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

from gin.

techjanitor avatar techjanitor commented on April 20, 2024

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")

from gin.

badoet avatar badoet commented on April 20, 2024

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..

from gin.

phrozen avatar phrozen commented on April 20, 2024

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.

from gin.

whidbey avatar whidbey commented on April 20, 2024

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.

from gin.

nazwa avatar nazwa commented on April 20, 2024

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)

from gin.

whidbey avatar whidbey commented on April 20, 2024

@nazwa and your settings detail?

from gin.

nazwa avatar nazwa commented on April 20, 2024

Anything you want really. In my case it's

settings := jpeg.Options{Quality: 80}

from gin.

whidbey avatar whidbey commented on April 20, 2024

@nazwa thankyou

from gin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.