Created
December 17, 2013 08:02
-
-
Save ialhashim/8001562 to your computer and use it in GitHub Desktop.
Box.com upload API using Qt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <QNetworkAccessManager> | |
#include <QNetworkRequest> | |
#include <QNetworkReply> | |
#include <QHttpMultiPart> | |
#include <QHttpPart> | |
#include <QFile> | |
namespace BoxCloud | |
{ | |
bool uploadFile( QString filename, QString accessToken, QString folderID = "871822902" ) | |
{ | |
QFile *fileUpload = new QFile(filename); | |
fileUpload->open(QIODevice::ReadOnly); | |
QHttpMultiPart * multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); | |
QByteArray boundary("56354654654654321768987465413574634354658"); | |
multiPart->setBoundary(boundary); | |
QHttpPart folderPart; | |
folderPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"folder_id\"")); | |
folderPart.setBody("871822902"); // folder at Box | |
QString fileExt = filename.split(".").takeLast(); | |
QString mimeType = "application/unknown"; | |
QString fileName = filename.split("/").takeLast(); | |
QHttpPart textPart; | |
textPart.setHeader(QNetworkRequest::ContentTypeHeader, mimeType); | |
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; filename=\""+ fileName +"\"; name=\"filename\";")); | |
textPart.setBodyDevice(fileUpload); | |
multiPart->append(textPart); | |
multiPart->append(folderPart); | |
QNetworkRequest req; | |
req.setUrl(QUrl("https://upload.box.com/api/2.0/files/content")); | |
QByteArray ba_auth = QString("Bearer " + accessToken).toUtf8(); | |
req.setRawHeader("Authorization", ba_auth); | |
QNetworkAccessManager * m_netmanager = new QNetworkAccessManager; | |
QNetworkReply * reply = m_netmanager->post(req, multiPart); | |
QEventLoop loop; | |
QObject::connect(reply, SIGNAL(readyRead()), &loop, SLOT(quit())); | |
loop.exec(); | |
// Lets print the HTTP GET response. | |
qDebug( reply->readAll() ); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This solution still works. But what aboute new API with such request
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 [email protected]
I've spend so much time trying to find solution but it was useless...