我有一個(gè) Go API,它應(yīng)該保存客戶端發(fā)送的圖像。我知道當(dāng) POST 請求來自 HTML 表單時(shí),Go 代碼有效。但是,當(dāng)從我的 Qt C++ 客戶端發(fā)送多部分發(fā)布請求時(shí),服務(wù)器返回錯(cuò)誤http: 沒有這樣的文件在客戶端,我有一個(gè) QPixmap,我將其轉(zhuǎn)換為 QByteArray,然后發(fā)送,但不知何故我從 Go 中得到了該錯(cuò)誤。我知道當(dāng)我刪除時(shí)客戶端發(fā)送的數(shù)據(jù)長度減少multi_part->append(image_part);所以應(yīng)該發(fā)送 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貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
我不確定,但你可以嘗試改變
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 關(guān)注
- 166 瀏覽
添加回答
舉報(bào)
0/150
提交
取消