自动文件上传到网站

时间:2016-11-06 13:15:28

标签: ruby-on-rails ubuntu web web-crawler image-uploading

我有一个网站,其中包含上传文件的表单。我希望在我的计算机上的本地文件夹上的场景发生更改后自动登录并上传图像文件。可以在这个问题上提供任何指导。

3 个答案:

答案 0 :(得分:0)

根据我的理解,您可以为此目的编写一个任务,可以运行让我们说每小时并检查目录中是否有任何更改,然后在您的应用程序上上传这些文件。

答案 1 :(得分:0)

我不知道你在做什么样的系统,但是你可以做这样的事情。如果您使用的是Linux系统,则可以使用watch命令跟踪所选目录的活动。您可以做的是在ruby脚本中使用类似Mechanize的内容,该脚本由watch命令触发,然后提交表单并通过选择具有最新创建日期的文件为您上传文件。 / p>

答案 2 :(得分:0)

我意识到它在帖子中表示ruby on rails,但这个答案与在Ruby中编写解决方案一样合理(并且更容易/更快)

使用Qt C ++执行此操作,您可以执行以下操作:

(未经测试,您必须根据具体情况进行调整)

代码概述:

  • 创建一个程序,每隔20分钟在一个Timer上循环,然后遍历您使用WATCH_DIR指定的整个目录,如果它找到该目录中的任何文件,这些文件在循环最后一次之间被修改运行(或在程序启动之后但在第一个循环运行之前),然后它将该确切文件上传到您使用UPLOAD_URL指定的任何URL

然后创建一个名为AutoUploader.pro的文件和一个名为main.cpp

的文件

<小时/> 的 AutoUploader.pro

QT += core network
QT -= gui

CONFIG += c++11

TARGET = AutoUploader
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

<小时/> 的 的main.cpp

#include <QtCore/QCoreApplication>
#include <QtCore/qglobal.h>
#include <QDir>
#include <QDirIterator>
#include <QNetworkAccessManager>
#include <QTimer>
#include <QByteArray>
#include <QHash>

#define WATCH_DIR "/home/lenny/images"
#define UPLOAD_URL "http://127.0.0.1/upload.php"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    MainLoop loop(WATCH_DIR);
    return a.exec();
}

class MainLoop : public QObject {
    Q_OBJECT
public: 
    MainLoop(QString _watch_directory = qApp->applicationDirPath()) {
        watch_directory = _watch_directory;


        // the  ACTION="" part of the upload form
        website_upload_url = UPLOAD_URL;

        /*  20 minutes
         20 * 60 * 1000 = num of milliseconds that makes up 
        20 mins =     1200000 ms */
        QTimer::singleShot(1200000, this, SLOT(check_for_updates()));

        /* this will stop any file modified before you ran this program from 
         being uploaded so it wont upload all of the files at runtime */
        program_start_time = QDateTime::currentDateTime();
    }


    QDateTime program_start_time;
    QString watch_directory;
    QString website_upload_url;

    // hash table to store all of the last modified times for each file 
    QHash<QString, QDateTime> last_modified_time;
    ~MainLoop() { qApp->exit(); }

public slots:
    void check_for_updates() {
        QDirIterator it(QDir(watch_directory));

        /* loop through all file in directory */
        while (it.hasNext()) {
            QFileInfo info(it.next());

            /* check to see if the files modified time is ahead of 
               program_start_time  */
            if (info.lastModified.msecsTo(program_start_time) < 1) {
                upload_file(info.absoluteFilePath());
            }
        }
        /* set program_start_time to the current time to catch stuff next
           time around and then start a timer to repeat this command in
           20 minutes */
        program_start_time = QDateTime::currentDateTime();
        QTimer::singleShot(1200000, this, SLOT(check_for_updates()));
    }


   /*  upload file  code came from 
      https://forum.qt.io/topic/11086/solved-qnetworkaccessmanager-uploading-files/2 
    */

    void upload_file(QString filename) {
        QNetworkAccessManager *am = new QNetworkAccessManager(this);
        QString path(filename);

        // defined with UPLOAD_URL
        QNetworkRequest request(QUrl(website_upload_url)); 

        QString bound="margin"; //name of the boundary
        //according to rfc 1867 we need to put this string here:
        QByteArray data(QString("--" + bound + "\r\n").toAscii());
        data.append("Content-Disposition: form-data; name=\"action\"\r\n\r\n");
        data.append("upload.php\r\n");
        data.append("--" + bound + "\r\n"); //according to rfc 1867
        data.append(QString("Content-Disposition: form-data; name=\"uploaded\"; filename=\"%1\"\r\n").arg(QFileInfo(filename).fileName()));     
        data.append(QString("Content-Type: image/%1\r\n\r\n").arg(QFileInfo(filename).suffix())); //data type
        QFile file(path);
        if (!file.open(QIODevice::ReadOnly))
            return;
        data.append(file.readAll()); //let's read the file
        data.append("\r\n");
        data.append("--" + bound + "--\r\n"); 
        request.setRawHeader(QString("Content-Type").toAscii(),QString("multipart/form-data; boundary=" + bound).toAscii());
        request.setRawHeader(QString("Content-Length").toAscii(), QString::number(data.length()).toAscii());
        this->reply = am->post(request,data);

        connect(this->reply, SIGNAL(finished()), this, SLOT(replyFinished()));   
    }

    void replyFinished() {
        /*  perform some code here whenever a download finishes */
    }

};

在运行此程序之前,请务必完全阅读它并通过阅读注释和帖子进行必要的更改 - 您可能还必须根据您的平台安装qt框架

无论如何,最后一步是运行qmake来创建项目makefile,最后是make来构建二进制文件。

显然,最后的步骤因您使用的系统而异。

...这个程序将继续运行...基本上永远运行直到你关闭它......每20分钟上传一次更改的文件

希望这会有所帮助......