pro
QT += core gui network
QT += script
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QFile>
#include <QDebug>
#include <QUrl>
#include <QString>
#include <QProgressBar>
#include <QMessageBox>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
QNetworkReply *reply;
QProgressBar *progressBar;
QFile *file;
public slots:
void replyFinished(QNetworkReply*);
void loadError(QNetworkReply::NetworkError);
void loadProgress(qint64 bytesSent,qint64 bytesTotal);
void readContent();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
};
#endif
.cp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDateTime>
#include <QtScript/QtScript>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::readContent()
{
file->write(reply->readAll());
qDebug()<<"jieshoutgfgdg"<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz dddd");
}
void MainWindow::replyFinished(QNetworkReply*)
{
if(reply->error() == QNetworkReply::NoError)
{
reply->deleteLater();
file->flush();
file->close();
qDebug()<<"jieshoutgsdd"<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz dddd");
}
else
{
progressBar->close();
QMessageBox::critical(NULL, tr("Error"), "Failed!!!");
}
}
void MainWindow::loadProgress(qint64 bytesSent, qint64 bytesTotal)
{
qDebug() << "loaded" << bytesSent << "of" << bytesTotal;
progressBar->setMaximum(bytesTotal);
progressBar->setValue(bytesSent);
}
void MainWindow::loadError(QNetworkReply::NetworkError)
{
qDebug()<<"Error: "<<reply->error();
}
void MainWindow::on_pushButton_clicked()
{
qDebug()<<"jieshoutgs"<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz dddd");
file = new QFile("D:/wamp64/www/holeLocking/upload/hello.png");
file->open(QIODevice::ReadOnly);
QByteArray byte_file="";
byte_file=file->readAll();
QNetworkAccessManager *accessManager2 = new QNetworkAccessManager(this);
accessManager2->setNetworkAccessible(QNetworkAccessManager::Accessible);
QUrl url2("http://192.168.0.113:80/holeLocking/upload.php?filename=hsello.png");
QNetworkRequest request2(url2);
request2.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
reply = accessManager2->post(request2, byte_file);
progressBar = new QProgressBar();
progressBar->setValue(0);
progressBar->show();
connect(accessManager2,SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(loadError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(uploadProgress(qint64 ,qint64)), this, SLOT(loadProgress(qint64 ,qint64)));
}
void MainWindow::on_pushButton_2_clicked()
{
qDebug()<<"jieshoutgd"<<QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz dddd");
file = new QFile("D:/wamp64/www/holeLocking/upload/1d3d52.doc");
file->open(QIODevice::WriteOnly);
QNetworkAccessManager *accessManager = new QNetworkAccessManager(this);
accessManager->setNetworkAccessible(QNetworkAccessManager::Accessible);
QUrl url("http://192.168.0.113:80/holeLocking/download.php?filename=23dd.doc");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/octet-stream");
reply = accessManager->get(request);
progressBar = new QProgressBar();
progressBar->setValue(0);
progressBar->show();
connect((QObject *)reply, SIGNAL(readyRead()), this, SLOT(readContent()));
connect(accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(loadError(QNetworkReply::NetworkError)));
connect(reply, SIGNAL(downloadProgress(qint64 ,qint64)), this, SLOT(loadProgress(qint64 ,qint64)));
}
php部分 download.php
<?php
$content = file_get_contents('php://input');
$foldername = "upload";
$filename = $_GET["filename"];
$fp = fopen($foldername."/".$filename,'w+');
fwrite($fp, $content, strlen($content));
fclose($fp);
?>
upload.php
<?php
$filename = $_GET["filename"];
$file_dir = "upload"."/".$filename;
$handler = fopen($file_dir, 'r+b');
$file_size = filesize($file_dir);
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".$file_size);
Header("Content-Disposition: attachment; filename=" . basename( $file_dir));
echo fread($handler,$file_size);
fclose($handler);
ob_end_flush();
exit;
?>
|