PaceyIV
14th April 2012, 16:10
I'm trying to use IDrive EVS REST APIs to upload a file with this service witout success.
This is the upload api (http://evs.idrive.com/web-file-upload-api.htm)
Upload local file(s) to your IDrive account
API Call: https://<server address>/evs/uploadFile
Method POST
Parameters uid=USERNAME&pwd=PASSWORD&pvtkey=OPTIONAL_PRIVATE_KEY&p=DESTPATH&myfiles=FILENAME
enctype multipart/form-data
I try this piece of code but doesn't work. IDrive says "REQUEST 'ENCTYPE' IS NOT MULTIPART FORM DATA"
QFile *file = new QFile(sourcePath);
file->open(QIODevice::ReadOnly);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart userNamePart;
userNamePart.setHeader(QNetworkRequest::ContentDis positionHeader, QVariant("form-data; name=\"uid\""));
userNamePart.setBody(this->_userName.toUtf8());
QHttpPart passwordPart;
passwordPart.setHeader(QNetworkRequest::ContentDis positionHeader, QVariant("form-data; name=\"pwd\""));
passwordPart.setBody(this->_password.toUtf8());
QHttpPart destPart;
destPart.setHeader(QNetworkRequest::ContentDisposi tionHeader, QVariant("form-data; name=\"p\""));
destPart.setBody(destPath.toUtf8());
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHe ader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispos itionHeader, QVariant("form-data; name=\"image\"; filename=\"" + file->fileName().toUtf8()+"\"\r\n\r\n\r\n"));
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(userNamePart);
multiPart->append(passwordPart);
multiPart->append(destPart);
multiPart->append(imagePart);
QNetworkRequest req;
req.setUrl(QUrl(QString("https://%0/evs/uploadFile").arg(this->_serverAddress)));
if (this->syncPost(req, multiPart)) {
return (this->_reply.result()["message"] == "SUCCESS");
}
return false;
// req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; charset=UTF-8; boundary=" + boundary);
// QFile file(sourcePath);
// file.open(QIODevice::ReadOnly);
// QByteArray postData(QString("uid=%0&pwd=%1&p=%3&myfiles=%4")
// .arg(this->_userName)
// .arg(this->_password)
// .arg(destPath)
// .arg(file.fileName()).toUtf8());
// postData.append("--" + boundary + "\r\n");
// postData.append("Content-Type: image/jpeg\r\n\r\n");
// postData.append(file.readAll());
// file.close(); // the file is opened earlier in the code
// postData.append("\r\n");
// postData.append("--" + boundary + "\r\n");
// int contentLength = postData.length();
// req.setHeader(QNetworkRequest::ContentLengthHeader , contentLength);
}
I also try with the commented code but I got the same error!
How should I do?
Albano
This is the upload api (http://evs.idrive.com/web-file-upload-api.htm)
Upload local file(s) to your IDrive account
API Call: https://<server address>/evs/uploadFile
Method POST
Parameters uid=USERNAME&pwd=PASSWORD&pvtkey=OPTIONAL_PRIVATE_KEY&p=DESTPATH&myfiles=FILENAME
enctype multipart/form-data
I try this piece of code but doesn't work. IDrive says "REQUEST 'ENCTYPE' IS NOT MULTIPART FORM DATA"
QFile *file = new QFile(sourcePath);
file->open(QIODevice::ReadOnly);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart userNamePart;
userNamePart.setHeader(QNetworkRequest::ContentDis positionHeader, QVariant("form-data; name=\"uid\""));
userNamePart.setBody(this->_userName.toUtf8());
QHttpPart passwordPart;
passwordPart.setHeader(QNetworkRequest::ContentDis positionHeader, QVariant("form-data; name=\"pwd\""));
passwordPart.setBody(this->_password.toUtf8());
QHttpPart destPart;
destPart.setHeader(QNetworkRequest::ContentDisposi tionHeader, QVariant("form-data; name=\"p\""));
destPart.setBody(destPath.toUtf8());
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHe ader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispos itionHeader, QVariant("form-data; name=\"image\"; filename=\"" + file->fileName().toUtf8()+"\"\r\n\r\n\r\n"));
imagePart.setBodyDevice(file);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
multiPart->append(userNamePart);
multiPart->append(passwordPart);
multiPart->append(destPart);
multiPart->append(imagePart);
QNetworkRequest req;
req.setUrl(QUrl(QString("https://%0/evs/uploadFile").arg(this->_serverAddress)));
if (this->syncPost(req, multiPart)) {
return (this->_reply.result()["message"] == "SUCCESS");
}
return false;
// req.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; charset=UTF-8; boundary=" + boundary);
// QFile file(sourcePath);
// file.open(QIODevice::ReadOnly);
// QByteArray postData(QString("uid=%0&pwd=%1&p=%3&myfiles=%4")
// .arg(this->_userName)
// .arg(this->_password)
// .arg(destPath)
// .arg(file.fileName()).toUtf8());
// postData.append("--" + boundary + "\r\n");
// postData.append("Content-Type: image/jpeg\r\n\r\n");
// postData.append(file.readAll());
// file.close(); // the file is opened earlier in the code
// postData.append("\r\n");
// postData.append("--" + boundary + "\r\n");
// int contentLength = postData.length();
// req.setHeader(QNetworkRequest::ContentLengthHeader , contentLength);
}
I also try with the commented code but I got the same error!
How should I do?
Albano