英文:
Gin-Gonic File upload mime error
问题
我正在使用gin-gonic包在Go语言中创建一个API,并且在文件上传处理程序中遇到了问题。以下是我的代码:
func postPicture(c *gin.Context) {
id, ok := c.Params.Get("fileId")
if !ok {...} // 错误处理
user, ok := c.Params.Get("user")
if !ok {...} // 错误处理
file, _, err := c.Request.FormFile("file") // 这里出现了错误
if err != nil {
Common.Debug("Error: " + err.Error())
c.JSON(http.StatusBadRequest, Common.JsonError{"Error", err.Error()})
return
} // 错误处理
path := "./Files/" + user + "/pictures"
filename := id + ".jpg"
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0755)
}
out, err := os.Create(path + "/" + filename)
if err != nil {...} // 错误处理
defer out.Close()
_, err = io.Copy(out, file)
if err != nil {...} // 错误处理
c.JSON(http.StatusAccepted, gin.H{})
}
错误出现在c.Request.FormFile()
这一行,无论请求是什么,它都会返回"mime: invalid media parameter"。我尝试了以下命令:
curl -X POST --form upload=C:\Users\meiche_j\Pictures\Capture.PNG -H "Content-Type: multipart/form-data;boundary=???" "http://127.0.0.1:3003/postFiles/picture/58cbb5627067500f58834f69/fileIdTest"
curl -X POST --form upload=C:\Users\meiche_j\Pictures\Capture.PNG -H "Content-Type: multipart/form-data;boundary=???;Content-Disposition: attachment; filename=file" "http://127.0.0.1:3003/postFiles/picture/58cbb5627067500f58834f69/fileIdTest"
curl -X POST --form upload=C:\Users\meiche_j\Pictures\Capture.PNG -H "Content-Type: multipart/form-data;boundary=???;Content-Disposition: form-data; filename=file" "http://127.0.0.1:3003/postFiles/picture/58cbb5627067500f58834f69/fileIdTest"
我不认为错误在代码中,但我找不到缺少的请求头是什么,有什么想法吗?
英文:
I'm creating an API in go with gin-gonic package, and I'm stuck with the file upload handler.
Here is my code:
func postPicture(c *gin.Context) {
id, ok := c.Params.Get("fileId")
if !ok {...} // Err Handling
user, ok := c.Params.Get("user")
if !ok {...} // Err Handling
file, _, err := c.Request.FormFile("file") // Here is the bug
if err != nil {
Common.Debug("Error: " + err.Error())
c.JSON(http.StatusBadRequest, Common.JsonError{"Error", err.Error()})
return
} // Err Handling
path := "./Files/" + user + "/pictures"
filename := id + ".jpg"
if _, err := os.Stat(path); os.IsNotExist(err) {
os.Mkdir(path, 0755)
}
out, err := os.Create(path + "/" + filename)
if err != nil {...} // Err Handling
defer out.Close()
_, err = io.Copy(out, file)
if err != nil {...} // Err Handling
c.JSON(http.StatusAccepted, gin.H{})
}
The error is on c.Request.FormFile(), it returns me "mime: invalid media parameter" whatever the request is. I tried the following:
curl -X POST --form upload=C:\Users\meiche_j\Pictures\Capture.PNG -H "Content-Type: multipart/form-data;boundary=???" "http://127.0.0.1:3003/postFiles/picture/58cbb5627067500f58834f69/fileIdTest"
curl -X POST --form upload=C:\Users\meiche_j\Pictures\Capture.PNG -H "Content-Type: multipart/form-data;boundary=???;Content-Disposition: attachment; filename=file" "http://127.0.0.1:3003/postFiles/picture/58cbb5627067500f58834f69/fileIdTest"
curl -X POST --form upload=C:\Users\meiche_j\Pictures\Capture.PNG -H "Content-Type: multipart/form-data;boundary=???;Content-Disposition: form-data; filename=file" "http://127.0.0.1:3003/postFiles/picture/58cbb5627067500f58834f69/fileIdTest"
I don't think the error is in the code, but I can't find what request headers are missing, any idea?
答案1
得分: 3
你在代码和测试中都犯了几个小错误:
-
在
c.Request.FormFile("file")
中,你应该使用正确的键名,这里你使用了file
作为键名,但是在你的 curl 请求中使用的是upload
,键名应该是--form upload=...
。 -
在你的 curl 请求中应该使用
@
符号:curl -X POST --form upload=@C:\Users\meiche_j\Pictures\Capture.PNG
,这样可以表示你要传输文件的内容,而不仅仅是文件路径。 -
你应该避免自己手动设置边界参数,在 curl 请求中可以这样发送请求:
curl -X POST -F upload=@pathtoyourfile -H 'Content-Type: multipart/form-data' "http://127.0.0.1:3003/postFiles/picture/58cbb5627067500f58834f69/fileIdTest"
希望这对你有帮助。
英文:
You make multiple little mistake in your code and in your test:
-
You should use the correct key in
c.Request.FormFile("file")
, here you usefile
as key but you useupload
in your curl request as key--form upload=...
-
You should use the
@
in your curl request :curl -X POST --form upload=@C:\Users\meiche_j\Pictures\Capture.PNG
to indicate that you want to transfer the content of the file and not only the path -
You should avoid putting by yourself the boundary parameter in your curl request and do a curl request like
curl -X POST -F upload=@pathtoyourfile -H 'Content-Type: multipart/form-data' "http://127.0.0.1:3003/postFiles/picture/58cbb5627067500f58834f69/fileIdTest"
Hope this has been usefull
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论