Gin-Gonic文件上传MIME错误

huangapple go评论82阅读模式
英文:

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

你在代码和测试中都犯了几个小错误:

  1. c.Request.FormFile("file") 中,你应该使用正确的键名,这里你使用了 file 作为键名,但是在你的 curl 请求中使用的是 upload,键名应该是 --form upload=...

  2. 在你的 curl 请求中应该使用 @ 符号:curl -X POST --form upload=@C:\Users\meiche_j\Pictures\Capture.PNG,这样可以表示你要传输文件的内容,而不仅仅是文件路径。

  3. 你应该避免自己手动设置边界参数,在 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:

  1. You should use the correct key in c.Request.FormFile("file"), here you use file as key but you use upload in your curl request as key --form upload=...

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

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

huangapple
  • 本文由 发表于 2017年3月17日 22:59:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/42860784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定