无法在 QT 4.8 中存储从 url 下载的文件
Unable to store Downloaded file from url in QT 4.8
我正在尝试使用 QT 4.8 中的 url 下载 xml 文件。这是我的代码:-
downloadurl::downloadurl(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QNetworkAccessManager GetFileDownloadNetworkManager;
QNetworkReply *GetFileDownloadNetworkReply;
QObject::connect(&GetFileDownloadNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));
QObject::connect(&GetFileDownloadNetworkManager, SIGNAL((QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));
QUrl url(QString("MyLinkToDownloadFile"));
QNetworkRequest reqpn(url);
reqpn.setRawHeader("Content-Type", "text/xml");
GetFileDownloadNetworkReply = GetFileDownloadNetworkManager.get(reqpn);
}
downloadurl::~downloadurl()
{
}
void downloadurl::FileDownloadReplyReceived(QNetworkReply *Reply)
{
if(Reply->error() == QNetworkReply::NoError)
{
QFile DownloadedFile("/FlashDisk/ServerTemp/12012.xml");
if (!DownloadedFile.open(QIODevice::WriteOnly))
{
messageBox.critical(0,"Error","Can't open File");
return;
}
DownloadedFile.write(Reply->readAll());
DownloadedFile.close();
}
Reply->deleteLater();
}
我的代码进入了插槽,但我在 /FlashDisk/ServerTemp/
中找不到创建的 12012.txt
。请告诉我这里到底缺少什么。
自己解决问题。我必须在 QFile.open
中添加 QIODevice::Text
权限。这是我的最终代码:-
downloadurl::downloadurl(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QNetworkAccessManager GetFileDownloadNetworkManager;
QNetworkReply *GetFileDownloadNetworkReply;
QObject::connect(&GetFileDownloadNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));
QObject::connect(&GetFileDownloadNetworkManager, SIGNAL((QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));
QUrl url(QString("MyLinkToDownloadFile"));
QNetworkRequest reqpn(url);
reqpn.setRawHeader("Content-Type", "text/xml");
GetFileDownloadNetworkReply = GetFileDownloadNetworkManager.get(reqpn);
}
downloadurl::~downloadurl()
{
}
void downloadurl::FileDownloadReplyReceived(QNetworkReply *Reply)
{
if(Reply->error() == QNetworkReply::NoError)
{
QFile DownloadedFile("/FlashDisk/ServerTemp/12012.xml");
if (!DownloadedFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
messageBox.critical(0,"Error","Can't open File");
return;
}
DownloadedFile.write(Reply->readAll());
DownloadedFile.close();
}
Reply->deleteLater();
}
我正在尝试使用 QT 4.8 中的 url 下载 xml 文件。这是我的代码:-
downloadurl::downloadurl(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QNetworkAccessManager GetFileDownloadNetworkManager;
QNetworkReply *GetFileDownloadNetworkReply;
QObject::connect(&GetFileDownloadNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));
QObject::connect(&GetFileDownloadNetworkManager, SIGNAL((QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));
QUrl url(QString("MyLinkToDownloadFile"));
QNetworkRequest reqpn(url);
reqpn.setRawHeader("Content-Type", "text/xml");
GetFileDownloadNetworkReply = GetFileDownloadNetworkManager.get(reqpn);
}
downloadurl::~downloadurl()
{
}
void downloadurl::FileDownloadReplyReceived(QNetworkReply *Reply)
{
if(Reply->error() == QNetworkReply::NoError)
{
QFile DownloadedFile("/FlashDisk/ServerTemp/12012.xml");
if (!DownloadedFile.open(QIODevice::WriteOnly))
{
messageBox.critical(0,"Error","Can't open File");
return;
}
DownloadedFile.write(Reply->readAll());
DownloadedFile.close();
}
Reply->deleteLater();
}
我的代码进入了插槽,但我在 /FlashDisk/ServerTemp/
中找不到创建的 12012.txt
。请告诉我这里到底缺少什么。
自己解决问题。我必须在 QFile.open
中添加 QIODevice::Text
权限。这是我的最终代码:-
downloadurl::downloadurl(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
QNetworkAccessManager GetFileDownloadNetworkManager;
QNetworkReply *GetFileDownloadNetworkReply;
QObject::connect(&GetFileDownloadNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));
QObject::connect(&GetFileDownloadNetworkManager, SIGNAL((QNetworkReply*)), this, SLOT(FileDownloadReplyReceived(QNetworkReply*)));
QUrl url(QString("MyLinkToDownloadFile"));
QNetworkRequest reqpn(url);
reqpn.setRawHeader("Content-Type", "text/xml");
GetFileDownloadNetworkReply = GetFileDownloadNetworkManager.get(reqpn);
}
downloadurl::~downloadurl()
{
}
void downloadurl::FileDownloadReplyReceived(QNetworkReply *Reply)
{
if(Reply->error() == QNetworkReply::NoError)
{
QFile DownloadedFile("/FlashDisk/ServerTemp/12012.xml");
if (!DownloadedFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
messageBox.critical(0,"Error","Can't open File");
return;
}
DownloadedFile.write(Reply->readAll());
DownloadedFile.close();
}
Reply->deleteLater();
}