Automatically upload data on server from selected directory path
-
@developerNancy Than you'll have to save a reference of the
QDir::entryInfoList
in a member variable and periotically callQDir::entryInfoList
to compare with your member variable List.If the latest call contains a file not in your m-Varibale, update your list and upload the new file.
wrote on 9 May 2017, 05:51 last edited by@J.Hilk After getting new file How to send it to server automatically?
-
@J.Hilk After getting new file How to send it to server automatically?
wrote on 9 May 2017, 06:11 last edited by@developerNancy said in Automatically upload data on server from selected directory path:
@J.Hilk After getting new file How to send it to server automatically?
@koahnig said in Automatically upload data on server from selected directory path:
There is also the QFileSystemWatcher which tells you when there is a change.
Follow the link given in the post above.
-
@J.Hilk After getting new file How to send it to server automatically?
I didn't knwo
QFileSystemWatcher
was a thing so I'll use that.//*.h #include <QFileSystemWatcher> ... private slots: void uploadViaQNetworkManager(QString filePath){//Your upload code;} void watcherSlot(QString s); private: QFileSystemWatcher watcher; QList<QString> m_refList;
//*.cpp watcher.addPath(PathToUploadFolder); QDir dir; dir.setPath(PathToUploadFolder); if(!dir.entryList(QDir::Files).isEmpty()) for(auto &str :dir.entryList(QDir::Files | QDir::NoDotAndDotDot)){ m_refList.append( str); connect(&watcher, &QFileSystemWatcher::directoryChanged, this, &myClass::watcherSlot); void myClass::watcherSlot(QString s){ QString newfile; QDir dir; dir.setPath(s); if(!dir.entryList(QDir::Files).isEmpty()) for(int i = 0; i < m_refList.size(); i++){ bool newEntry = true;; for(newfile : dir.entryList(QDir::Files | QDir::NoDotAndDotDot)){ if(m_refList.at(i) == newfile) newEntry = false; if(newEntry){ uploadViaQNetworkManager(s+"\n"+newfile); m_refList.append(newfile); } } }
Disclaimer this is pseudo code and untested. But the prinziple should be valid
Edit: changed the filter from dirs to files.
-
@J.Hilk After getting new file How to send it to server automatically?
@developerNancy said in Automatically upload data on server from selected directory path:
After getting new file How to send it to server automatically?
Didn't you say in your first post that you already implemented uploading to the server?
-
@developerNancy said in Automatically upload data on server from selected directory path:
After getting new file How to send it to server automatically?
Didn't you say in your first post that you already implemented uploading to the server?
wrote on 9 May 2017, 09:57 last edited by@jsulm Yes I have done with Uploading Multiple files to server Now I want to implement background sync. like if any file copy in selected folder than it automatically uploaded to server. For this watcher or threading which is the best solution.....
-
@jsulm Yes I have done with Uploading Multiple files to server Now I want to implement background sync. like if any file copy in selected folder than it automatically uploaded to server. For this watcher or threading which is the best solution.....
@developerNancy That I understand. What I don't understand: if you're able to upload multiple files why can't you upload one? So, what exactly is the problem?
-
@developerNancy That I understand. What I don't understand: if you're able to upload multiple files why can't you upload one? So, what exactly is the problem?
wrote on 9 May 2017, 12:34 last edited by@jsulm I want to sync data in background it automatically uploaded when new file is added in directory no need to click again upload button.
-
@jsulm I want to sync data in background it automatically uploaded when new file is added in directory no need to click again upload button.
@developerNancy That I understand. You already was pointed to QFileSystemWatcher, @J-Hilk even provided code. So, what is not working?
-
@developerNancy That I understand. You already was pointed to QFileSystemWatcher, @J-Hilk even provided code. So, what is not working?
wrote on 9 May 2017, 13:23 last edited by@jsulm which is the best approach for background sync I am Implementing thread ...
-
I didn't knwo
QFileSystemWatcher
was a thing so I'll use that.//*.h #include <QFileSystemWatcher> ... private slots: void uploadViaQNetworkManager(QString filePath){//Your upload code;} void watcherSlot(QString s); private: QFileSystemWatcher watcher; QList<QString> m_refList;
//*.cpp watcher.addPath(PathToUploadFolder); QDir dir; dir.setPath(PathToUploadFolder); if(!dir.entryList(QDir::Files).isEmpty()) for(auto &str :dir.entryList(QDir::Files | QDir::NoDotAndDotDot)){ m_refList.append( str); connect(&watcher, &QFileSystemWatcher::directoryChanged, this, &myClass::watcherSlot); void myClass::watcherSlot(QString s){ QString newfile; QDir dir; dir.setPath(s); if(!dir.entryList(QDir::Files).isEmpty()) for(int i = 0; i < m_refList.size(); i++){ bool newEntry = true;; for(newfile : dir.entryList(QDir::Files | QDir::NoDotAndDotDot)){ if(m_refList.at(i) == newfile) newEntry = false; if(newEntry){ uploadViaQNetworkManager(s+"\n"+newfile); m_refList.append(newfile); } } }
Disclaimer this is pseudo code and untested. But the prinziple should be valid
Edit: changed the filter from dirs to files.
wrote on 9 May 2017, 16:39 last edited by@J.Hilk Can you please explain How I get the Path and watcher active after button click.
-
@J.Hilk Can you please explain How I get the Path and watcher active after button click.
@developerNancy you don't share any code sample with us, so it's hard to give definite working answers. You'll have to take what we write and integrate into your project.
That said I gave you a rough example how to setup and use the watcher.
@J.Hilk said in Automatically upload data on server from selected directory path:
//*.h QFileSystemWatcher watcher;
//*.cpp watcher.addPath(PathToUploadFolder); QDir dir; dir.setPath(PathToUploadFolder); if(!dir.entryList(QDir::Files).isEmpty()) for(auto &str :dir.entryList(QDir::Files | QDir::NoDotAndDotDot)){ m_refList.append( str); connect(&watcher, &QFileSystemWatcher::directoryChanged, this, &myClass::watcherSlot);
@developerNancy said in Automatically upload data on server from selected directory path:
How I get the Path
I don't know! How do you specify the folder for the multi file upload to the server? Just copy that path to a Qstring and give it to your
QFileSystemWatcher
-Instance
16/17