Uploading Directory/Folder to a server
-
I've wrote a class for uploading/downloading files using QNetworkAccessManager and its working fine but now I want to add "uploading directory" functionality in it.
Here is how I'm uploading a single file
void HTTP::UploadFile (QNetworkRequest request, QFileInfo filePath){ QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart dataPart; dataPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application")); dataPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=file; filename=\"" + filePath.fileName() + "\"")); QFile *file = new QFile(filePath.absoluteFilePath()); file->open(QIODevice::ReadOnly); dataPart.setBodyDevice(file); file->setParent(multiPart); multiPart->append(dataPart); QNetworkReply *reply = UploadManager.post(request, multiPart); connect(reply, &QNetworkReply::uploadProgress, this, &HTTP::showProgress); }Although I can iterate through files (within the directory) and upload individual files like below:-
QDirIterator it(DirPath, QStringList() << "*.*", QDir::Files, QDirIterator::Subdirectories); while (it.hasNext()) UploadFile (request, QFileInfo {it.next()});But it will just send files and NOT the directory structure. I want the files to be uploaded along the subdirectories.
For example; If i want to upload a ParentDir. I would want to upload the ParentDir along with its subdirectories and files.
How can I achieve that ?
-
I've wrote a class for uploading/downloading files using QNetworkAccessManager and its working fine but now I want to add "uploading directory" functionality in it.
Here is how I'm uploading a single file
void HTTP::UploadFile (QNetworkRequest request, QFileInfo filePath){ QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart dataPart; dataPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application")); dataPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=file; filename=\"" + filePath.fileName() + "\"")); QFile *file = new QFile(filePath.absoluteFilePath()); file->open(QIODevice::ReadOnly); dataPart.setBodyDevice(file); file->setParent(multiPart); multiPart->append(dataPart); QNetworkReply *reply = UploadManager.post(request, multiPart); connect(reply, &QNetworkReply::uploadProgress, this, &HTTP::showProgress); }Although I can iterate through files (within the directory) and upload individual files like below:-
QDirIterator it(DirPath, QStringList() << "*.*", QDir::Files, QDirIterator::Subdirectories); while (it.hasNext()) UploadFile (request, QFileInfo {it.next()});But it will just send files and NOT the directory structure. I want the files to be uploaded along the subdirectories.
For example; If i want to upload a ParentDir. I would want to upload the ParentDir along with its subdirectories and files.
How can I achieve that ?
@pingal
You need to recurse down your source directory structure, callingUploadFile()for files. You need to test/get yourQDirIteratorto return directories as well as files. You may want to write anUploadDirectory()method. And of course you need to ensure you tell the server to put files in the appropriate directory there, as necessary. -
So it means that first I've to get only Directories/SubDir list and send it to the server (so that it can make those directories) and then send individual files to the appropriate location.
@pingal
Not necessarily. That depends on your server side, where it puts the files you send it. I'm not even sure your server side will do "creating a hierarchy" for the files/file paths you send it, it might put them all "flat" in one directory. You must check that out. -
@pingal
Not necessarily. That depends on your server side, where it puts the files you send it. I'm not even sure your server side will do "creating a hierarchy" for the files/file paths you send it, it might put them all "flat" in one directory. You must check that out.