根据Box API上传文件,这是curl命令:
curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer ACCESS_TOKEN" -X POST \
-F attributes='{"name":"tigers.jpeg", "parent":{"id":"11446498"}}' \
-F file=@myfile.jpg
我正在尝试使用QT上传图片,以下是我的代码:
QUrl url(QString::fromLatin1("https://upload.box.com/api/2.0/files/content"));
auto* multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart attributes;
attributes.setHeader(QNetworkRequest::ContentDispositionHeader,
QLatin1String("form-data; name=\"attributes\""));
QByteArray postData = QString::fromUtf8("{\"name\":\"%1\",\"parent\":{\"id\":\"%2\"}}").arg(QFileInfo(imgPath).fileName()).arg(id).toUtf8();
attributes.setBody(postData);
multipart->append(attributes);
QFile* file = new QFile(imgPath);
file->open(QIODevice::ReadOnly);
file->setParent(multipart);
QHttpPart imagepart;
imagepart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagepart.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant(QString::fromLatin1("form-data; name=\"image\";filename=\"%1\"").arg(QLatin1String(QFileInfo(imgPath).fileName().toUtf8()))));
imagepart.setHeader(QNetworkRequest::ContentLengthHeader, file->size());
imagepart.setBodyDevice(file);
multipart->append(imagepart);
QNetworkRequest netRequest(url);
netRequest.setHeader(QNetworkRequest::ContentTypeHeader, QLatin1String("multipart/form-data"));
netRequest.setRawHeader("Authorization", QString::fromLatin1("Bearer {%1}").arg(d->o2->token()).toUtf8());
d->reply = d->netMngr->post(netRequest, multipart);
multipart->setParent(d->reply);
当我尝试上传图片时,出现错误提示
传输https://upload.box.com/api/2.0/files/content时出错- 服务器回复:错误的请求
我在做什么错了?