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)
- Pass variable in rout HOT 1
- Register tauri:// scheme (or allow custom schemes upstream?)
- please support get parms from Transfer-Encoding:compress-deflate
- unexpected EOF during install HOT 1
- sse panic on fasthttphandler HOT 1
- Will it be made into a more comprehensive framework later on? HOT 4
- How does the gin close the log file? HOT 3
- Should we use url.PathUnescape instead of url.QueryUnescape when UnescapePathValues is true
- go get failed: The requested name is valid, but no data of the requested type was found HOT 1
- Attach Response Headers (or middlewares) to redirected requests (CORS issues) HOT 1
- Should context.GetRawData() also copy body to BodyBytesKey? HOT 3
- YAML BindBody should not parse JSON content HOT 1
- Gin do not support javascript ? HOT 5
- Does gin.Recovery() run before or after Group middleware? HOT 1
- How can i prevent a http connection to close HOT 3
- 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 3
- [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
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.