1
我想上傳圖像到谷歌驅動器在QT/C + +。 我的代碼:http://pastebin.com/3CVcY3Rv無效的multipart請求與0 mime部分谷歌驅動器上傳qt(解決)
這是我做到了在qt.Now我用我上傳圖片到我的谷歌驅動this.Hopefully這有助於:
void googled::tryMultipartUpload(QNetworkReply *reply){
QByteArray d = reply->readAll();
qDebug() << d;
QString x = getValue(d,"access_token");
qDebug() << x;
x = "Bearer " + x;
am = new QNetworkAccessManager;
QUrl url("https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart");
QNetworkRequest request;
request.setUrl(url);
QString bound = "asd";
QByteArray data(QString("--"+bound+"\r\n").toAscii());
data.append("Content-Type: application/json; charset=UTF-8\r\n\r\n");
data.append("{\"title\":\"Meta\"}\r\n");
data.append("--"+bound+"\r\n");
data.append("Content-Type: image/jpeg\r\n\r\n");
QFile file("/home/saurabh/Pictures/08_wallpaper_mount_everest.jpg");
file.open(QIODevice::ReadOnly);
data.append(file.readAll());
data.append("\r\n");
data.append("--"+bound+"--\r\n");
request.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/related;boundary="+bound).toAscii());
request.setRawHeader(QString("Content-Length").toAscii(),QString("2000000").toAscii());
request.setRawHeader(QString("Host").toAscii(),QString("www.googleapis.com").toAscii());
request.setRawHeader(QString("Authorization").toAscii(),x.toAscii());
qDebug() << data.data();
QObject::connect(am, SIGNAL(finished(QNetworkReply *)),
this, SLOT(uploadfinishedSlot(QNetworkReply *)));
am->post(request,data);
}
代碼中有許多錯誤。你不應該使用'str + = QString(「some string」)。toAscii()',這是完全不正確的(toAscii()結果會再次轉換爲QString),'str + = QString(「some string」) '夠了。 我不確定多部分請求可以包含二進制數據。不應該在BASE64中編碼數據嗎? 用作邊界字符串「42 + 13」(帶空格)和「42 + 13」(不帶空格)。他們必須是平等的。 –
@ york.beta我改變了代碼,因爲你建議,但我得到的錯誤:格式錯誤的多ip body.My代碼更改後:http://pastebin.com/mbApz4W7 – saurabh