1

I am trying to upload image to google-drive in qt/c++. My code:

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);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
saurabh
  • 110
  • 3
  • 12
  • 2
    There are a number of errors in your code. You shouldn't use `str += QString("some string").toAscii()`, it's totally incorrect (toAscii() result will be converted to QString again), `str += QString("some string")` is enough. I'm not sure that a multipart request can contain a binary data. Shouldn't the data be encoded in BASE64? You use as boundary string "42 + 13" (with spaces) and "42+13" (without spaces). They must be equal. – york.beta Jun 18 '13 at 07:23
  • @york.beta I changed the code as you suggested but I get error:Malformed mulipart body.My code after changes:http://pastebin.com/mbApz4W7 – saurabh Jun 21 '13 at 06:44
  • I've rollback your replacement of the question by the solution. Please find [your solution in the revision history](https://stackoverflow.com/revisions/f0289986-fda3-4db9-8522-3f939dbefbf9/view-source) and post it as an answer of its own. – Cœur May 13 '18 at 14:57

1 Answers1

2

You are not using the quotation marks for the boundary attribute. Use the following content-type:

QString y = "multipart/related; boundary=\"" + QString("42+13") + "\"";
Burcu Dogan
  • 9,153
  • 4
  • 34
  • 34
  • I changed the code but I get error:malformed multipart body.My code after changes:http://pastebin.com/mbApz4W7 Any help – saurabh Jun 21 '13 at 07:18