所以我从gmail api下载附件时遇到问题。代码基本上是这样的:
QJsonDocument doc;
QUrl url("https://www.googleapis.com/gmail/v1/users/me/messages/12d144feec288dae/attachments/ANGjdJ8ZGywUR2NG7G0rIXH-mVGNHcFRnjlbz71ZtyUM3zn-sZtPHXVE5T5TqwzkFHovu7mB2zA2wqjIHNA8ysiUPmPbrVoKveuWJjaLFXky0STyESh4uxOOz2933W4uMI6PsynBHS4cpVNMBvSa");
QNetworkRequest request;
request.setRawHeader("Authorization", "Bearer" + myToken);
request.setUrl(url);
QNetworkAccessManager manager;
QNetworkReply *reply = manager.get(request);
QEventLoop wait;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &wait, SLOT(quit()));
QTimer::singleShot(5000, &wait, SLOT(quit()));
wait.exec();
doc = QJsonDocument::fromJson(reply->readAll());
//this is working wrong
QString str = doc.object().value("data").toString();
QByteArray arr;
arr.append(str);
QByteArray b64 = QByteArray::fromBase64(arr);
QByteArray fb64 = arr.toBase64();
QFile f("Math.rar");
f.open(QIODevice::WriteOnly);
f.write(b64);
f.close();
我知道它不是最好的代码,但我只想知道如何从Base64正确保存文件。有人可以帮忙吗?
答案 0 :(得分:0)
我解决了这个问题。该交易是在函数QByteArray :: fromBase64中添加QByteArray :: Base64UrlEncoding。所以,这个例子就是这样的:
QString str = doc.object().value("data").toString(); //getting a binary data from Json
QByteArray arr;
arr.append(str);
QByteArray b64 = QByteArray::fromBase64(arr, QByteArray::Base64UrlEncoding);
QFile f("Math.rar");
f.open(QIODevice::WriteOnly);
f.write(b64);
f.close();