简体   繁体   English

(Qt或其他)在Windows桌面或explorer.exe上使用协议http或ftp拖放mime类型uri-list的数据

[英](Qt or other) drag and drop data of mime-type uri-list with protocole http or ftp on Windows desktop or explorer.exe

How can I drag and drop a file from a protocol http and ftp to the Windows desktop or explorer.exe with Qt or any other library or language you use? 如何使用Qt或您使用的任何其他库或语言将文件从协议http和ftp拖放到Windows桌面或explorer.exe? It would be the same thing that I'm looking for. 这就是我要寻找的东西。

For example: You can drag an Image using http protocol From Chrome browser and drop it to the Windows desktop. 例如:您可以使用http协议从Chrome浏览器中拖动图像并将其拖放到Windows桌面。

My code works only with file:/// protocol. 我的代码仅适用于file:///协议。 This code does not work. 此代码不起作用。

QMimeData *mime = new QMimeData;
QList<QUrl> urls;
urls.push_back(QUrl("http://localhost/test/images.jpg"));
            mime->setUrls(urls);
            drag.setMimeData(mime);
            drag.setPixmap(QPixmap(":/Image (139).jpg"));
            drag.exec(Qt::MoveAction | Qt::CopyAction);

But this works fine: 但这很好用:

QMimeData *mime = new QMimeData;
            QList<QUrl> urls;
            urls.push_back(QUrl("file:///c:/images.jpg"));
            mime->setUrls(urls);
            drag.setMimeData(mime);
            drag.setPixmap(QPixmap(":/Image (139).jpg"));
            drag.exec(Qt::MoveAction | Qt::CopyAction);

Save file from web to local computer (temp directory) and use method with file:/// protocol. 将文件从Web保存到本地计算机(临时目录),然后使用file:///协议使用方法。

void MainWindow::downloadToTemp(QString p_FilePath)
{
    QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(p_FilePath)));
    connect(reply, SIGNAL(finished()), this, SLOT(httpDownloadFinished()));
}

void MainWindow::httpDownloadFinished() {
    QNetworkReply *reply = qobject_cast<QNetworkReply*>(QObject::sender());
    QString url = reply->url().toString();
    qDebug() << url;
    QString fileName = QDir::tempPath() + url.right(url.length() - url.lastIndexOf('/'));
    QFile file(fileName);
    if (file.open(QFile::WriteOnly)) {
        file.write(reply->readAll());
        file.flush();
        file.close();

        //Your file path - fileName
    }
}

Do not forget about error processing: QNetworkReply::error(QNetworkReply::NetworkError code) 不要忘记错误处理: QNetworkReply::error(QNetworkReply::NetworkError code)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM