Gin Gonic文件上载mime错误
我在go with gin gonic包中创建了一个API,我一直在使用文件上传处理程序。 这是我的密码:Gin Gonic文件上载mime错误,go,file-upload,go-gin,Go,File Upload,Go Gin,我在go with gin gonic包中创建了一个API,我一直在使用文件上传处理程序。 这是我的密码: 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
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{})
}
错误发生在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"
我认为代码中没有错误,但我找不到缺少的请求头,知道吗?您在代码和测试中犯了多个小错误:
c.Request.FormFile(“file”)
中使用正确的键,这里您使用file
作为键,但在curl请求中使用upload
作为键--form upload=…
@
:curl-X POST--form upload=@C:\Users\meiche\u j\Pictures\Capture.PNG
来指示您要传输文件的内容,而不仅仅是路径curl -X POST -F upload=@pathtoyourfile -H 'Content-Type: multipart/form-data' "http://127.0.0.1:3003/postFiles/picture/58cbb5627067500f58834f69/fileIdTest"
希望这是有用的您在代码和测试中犯了多个小错误:
c.Request.FormFile(“file”)
中使用正确的键,这里您使用file
作为键,但在curl请求中使用upload
作为键--form upload=…
@
:curl-X POST--form upload=@C:\Users\meiche\u j\Pictures\Capture.PNG
来指示您要传输文件的内容,而不仅仅是路径curl -X POST -F upload=@pathtoyourfile -H 'Content-Type: multipart/form-data' "http://127.0.0.1:3003/postFiles/picture/58cbb5627067500f58834f69/fileIdTest"
希望这是有用的你能尝试使用“强制”将“内容”部分变成文件,在文件名前面加上一个“@sign”,即curl-X POST--form file=@C:\Users\meiche\u j\Pictures\Capture.PNG……你能尝试使用“强制”将“内容”部分变成文件吗,在文件名前面加上一个“@sign”,即curl-X POST--form file=@C:\Users\meiche\u j\Pictures\Capture.PNG。。。。