【问题标题】:Invalid multipart request with 0 mime parts google drive upload无效的多部分请求,包含 0 个 mime 部分谷歌驱动器上传
【发布时间】:2013-06-18 06:59:36
【问题描述】:

我正在尝试将图像上传到 qt/c++ 中的 google-drive。 我的代码:

void googled::newuploadSettings(QNetworkReply *reply){
    QByteArray m_boundary;
    m_boundary  = "--";
    m_boundary += QString("42 + 13").toAscii();

    QByteArray data = reply->readAll();
    qDebug() << data;
    QString x = getValue(data,"access_token");
    qDebug() << x;
    x = "Bearer " + x;
    qDebug() << x;
    QNetworkRequest request;
     QUrl url("https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart");

    request.setUrl(url);
    request.setRawHeader("Content-Length","200000000");
    QString y = "multipart/related; boundary=" + QString("42+13");
    qDebug() << y;
    request.setRawHeader("Content-Type",y.toAscii());
    request.setRawHeader("Authorization",x.toLatin1());

    QString str;
    str += m_boundary;
    str += "\r\n";
    str += "Content-Disposition: form-data; title=\"";
    str += QString("sp").toAscii();
    str += "\"; ";
    str += "filename=\"";
    str += QFile::encodeName("kashmir");
    str += "\"\r\n";
    str += "Content-Length: " ;
    str +=  QString("200000000").toAscii();
    str += "\r\n";
    str += "Content-Type: ";
    str +=  QString("application/json; charset=UTF-8").toAscii();
    str += "\r\n";
    str += "Mime-version: 1.0 ";
    str += "\r\n";

    str += "\r\n";
    str += "mimeType:image/jpeg";
    str += "\r\n";

    str += "\r\n\r\n";


    str += m_boundary;

    str += "Content-Type: ";
    str += "image/jpeg";


    QByteArray arr;
    arr.append(str.toUtf8());
    QFile file("/home/saurabh/Pictures/005.jpg");
    file.open(QIODevice::ReadOnly);


    arr.append(file.readAll());
    arr.append(m_boundary);
    file.close();
    qDebug() << "file";
    //qDebug() << str;
    qDebug() << arr;
    m_netM = new QNetworkAccessManager;
    QObject::connect(m_netM, SIGNAL(finished(QNetworkReply *)),
    this, SLOT(uploadfinishedSlot(QNetworkReply *)));

    m_netM->post(request,arr);
}

【问题讨论】:

  • 您的代码中有许多错误。你不应该使用str += QString("some string").toAscii(),这是完全不正确的(toAscii() 结果将再次转换为QString),str += QString("some string") 就足够了。我不确定多部分请求是否可以包含二进制数据。数据不应该用BASE64编码吗?您使用“42 + 13”(带空格)和“42+13”(不带空格)作为边界字符串。它们必须相等。
  • @york.beta 我按照您的建议更改了代码,但出现错误:Malformed multipart body.My code after changes:pastebin.com/mbApz4W7
  • 我已经通过解决方案回滚了您对问题的替换。请找到your solution in the revision history 并将其作为自己的答案发布。

标签: qt google-drive-api google-drive-realtime-api


【解决方案1】:

您没有为边界属性使用引号。使用以下内容类型:

QString y = "multipart/related; boundary=\"" + QString("42+13") + "\"";

【讨论】:

  • 我更改了代码,但出现错误:malformed multipart body.My code after changes:pastebin.com/mbApz4W7 Any help
猜你喜欢
  • 2018-07-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
相关资源
最近更新 更多