Comments (13)
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.
@manucorporat Add this feature is cool. but is better to be a middleware.
from gin.
@johndeng I agree
from gin.
@manucorporat Thanks.
from gin.
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.
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.
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.
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.
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.
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.
@nazwa and your settings detail?
from gin.
Anything you want really. In my case it's
settings := jpeg.Options{Quality: 80}
from gin.
@nazwa thankyou
from gin.
Related Issues (20)
- BasicAuth is not suitable for proxy auth
- Can't get status as expected from context.Status() HOT 1
- Gin Context implementation breaks context.Context contract HOT 4
- [Suggestions] Introducing an AI-powered robot to assist with daily issue and pull request (PR) tasks.
- Need c.JSON() response like behaviour for c.SSEvent() response for supporting first party clients like OpenAI SDK HOT 2
- custom time.Time type can not use bindQuery get a value. HOT 3
- configure server parameters HOT 1
- The binding:"required" tag does not seem to work for struct types HOT 3
- Is there a way to preserve order in JSON on response? HOT 1
- redirect is not success HOT 1
- I want to use shouldmindbodywith first and then use formafile, but it doesn't work right now HOT 3
- Extend the routing tree entry
- Transparent decompression for gzip, deflate, etc? HOT 1
- Custom time.Time type can not use bindQuery get a value 2 HOT 2
- http: response.Write on hijacked connection from github.com/gin-gonic/gin.(*responseWriter).Write (response_writer.go:83) HOT 1
- Suggestion: Move Print Debug to Run() functions? HOT 7
- AbortWithStatusJSON doesn't abort context HOT 2
- I translated `tree.go` into java
- GetUint and GetUint64 wrong return HOT 1
- Multipart form-data request with empty key for single file upload
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gin.