dtv7174 2016-01-29 11:39
浏览 76
已采纳

从HTTP发送图像到HTTP后没有http:

I have a Go API that should save an image sent by a client. I know that the Go code works when the POST request comes from a HTML form. When sending a multipart post request from my Qt C++ client though, the server returns an error

http: no such file

On the client side I have a QPixmap, which I transform to a QByteArray, which I then send, but somehow I get that error from Go. I know that the length of the data sent by the client decreases when I remove

multi_part->append(image_part);

so the QPixmap should be sent.

Go code:

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)
}

}

Qt C++ client code:

void Share::upload(const QByteArray &data) {
    QHttpMultiPart *multi_part = new          QHttpMultiPart(QHttpMultiPart::FormDataType);
    QHttpPart text_part;
    text_part.setHeader(QNetworkRequest::ContentDispositionHeader,   QVariant("form-data; name=\"text\"\""));
    text_part.setBody("Screenshot.png");
    QHttpPart image_part;
    image_part.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
    image_part.setHeader(QNetworkRequest::ContentDispositionHeader,  QVariant("form-data; name=\"image\""));
    image_part.setBody(data);
    multi_part->append(text_part);
    multi_part->append(image_part);
    QNetworkRequest request(QUrl("http://localhost:8000/api/upload"));
    QNetworkAccessManager *manager = new QNetworkAccessManager();
    connect(manager, SIGNAL(finished(QNetworkReply *)), this,    SLOT(reply_finished(QNetworkReply *)));
    manager->post(request, multi_part);
    qDebug() << QString(data);
}
  • 写回答

1条回答 默认 最新

  • dongxing1412 2016-01-29 13:28
    关注

    I'm not sure but you can try to change

    image_part.setHeader(QNetworkRequest::ContentDispositionHeader,  QVariant("form-data; name=\"image\""));
    

    to something like

    image_part.setHeader(QNetworkRequest::ContentDispositionHeader,  QVariant("form-data; name=\"image\"; filename=\"Screenshot.png\""));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题