我有一个 Go API,它应该保存客户端发送的图像。我知道当 POST 请求来自 HTML 表单时,Go 代码有效。但是,当从我的 Qt C++ 客户端发送多部分发布请求时,服务器返回错误http: 没有这样的文件在客户端,我有一个 QPixmap,我将其转换为 QByteArray,然后发送,但不知何故我从 Go 中得到了该错误。我知道当我删除时客户端发送的数据长度减少multi_part->append(image_part);所以应该发送 QPixmap。去代码:func apiUploadHandler(w http.ResponseWriter, req *http.Request) {if req.Method == "POST" { req.ParseMultipartForm(0) fmt.Printf("%v %v %v", req.RemoteAddr, req.ContentLength, req.Body) file, fileheader, err := req.FormFile("image") if err != nil { cio.PrintMessage(1, err.Error()) return } defer file.Close() var id string created := false for created != true { id = generateImageID() err = db.CheckIfImageIDInUse(id) if err != nil { if err.Error() == "Image ID '"+id+"' exists.'" { created = false continue } else { created = false cio.PrintMessage(1, err.Error()) return } } created = true } filePath := fsys.ImgStoragePath + id + "." + strings.Split(fileheader.Filename, ".")[1] err = db.StoreImage(id, strings.Split(fileheader.Filename, ".")[0] /*image name*/, filePath, strings.Split(fileheader.Filename, ".")[1] /*extension*/) if err != nil { cio.PrintMessage(1, err.Error()) return } bytesCopied, err := fsys.StoreImage(filePath, file) if err != nil { cio.PrintMessage(1, err.Error()) return } cio.PrintMessage(0, "File "+filePath+" has been created.") if err != nil { cio.PrintMessage(1, err.Error()) return } cio.PrintMessage(0, "Content of uploaded image ("+strconv.FormatInt(bytesCopied, 10)+" Bytes) has been copied to "+filePath+".") http.Redirect(w, req, "/"+id, http.StatusFound)}}
1 回答
慕的地10843
TA贡献1785条经验 获得超8个赞
我不确定,但你可以尝试改变
image_part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\""));
像
image_part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\"; filename=\"Screenshot.png\""));
- 1 回答
- 0 关注
- 144 浏览
添加回答
举报
0/150
提交
取消