小凡慢慢学
慢慢学,更有趣
 1 class QKUploadFile: public QWidget
 2 {
 3     Q_OBJECT
 4 public:
 5     explicit QKUploadFile(QWidget *parent = 0);
 6     ~QKUploadFile();
 7     void uploadFile(QString uploadFilename);
 8     static QKUploadFile * getInstance();
 9 private:
10     QNetworkAccessManager *_uploadManager;
11     QNetworkReply *_reply;
12     char *m_buf;
13     QString filename;
14     static QKUploadFile *d;
15 
16 private slots:
17     void replyFinished(QNetworkReply *reply);
18     void upLoadError(QNetworkReply::NetworkError errorCode);
19     void OnUploadProgress(qint64 bytesSent, qint64 bytesTotal);
20 };

 

 1 QKUploadFile *QKUploadFile::d = 0;
 2 
 3 QKUploadFile::QKUploadFile(QWidget *parent)
 4     :QWidget(parent)
 5 {
 6 //    QFileDialog dialog(this);
 7 //    filename = dialog.getOpenFileName(0, "/");
 8 
 9     _uploadManager = new QNetworkAccessManager(this);
10     connect(_uploadManager,SIGNAL(finished(QNetworkReply*)),SLOT(replyFinished(QNetworkReply*)));
11     m_buf = NULL;
12 }
13 
14 QKUploadFile::~QKUploadFile()
15 {
16     if( m_buf != NULL ) delete[] m_buf;
17 }
18 
19 QKUploadFile * QKUploadFile::getInstance()
20 {
21     if (d == 0)
22         d = new QKUploadFile;
23     return d;
24 }
25 
26 
27 void QKUploadFile::uploadFile(QString uploadFilename)
28 {
29     QFile file(uploadFilename);
30     file.open(QIODevice::ReadOnly);
31     int  file_len = file.size();
32 
33     QDataStream in(&file);
34     m_buf = new char[file_len];
35     in.readRawData( m_buf, file_len);
36     file.close();
37 
38     QUrl url = QString("http://10.0.1.77:8080/TrainService/Uploadfile");
39     QNetworkRequest request( url );
40 
41     request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data");
42     request.setRawHeader("filename", uploadFilename.section('/', -1, -1).toUtf8() );
43 
44     QByteArray arr = QByteArray( m_buf, file_len );
45     _reply = _uploadManager->post( request , arr );
46 
47     connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(upLoadError(QNetworkReply::NetworkError)));
48     connect(_reply, SIGNAL(uploadProgress ( qint64 ,qint64 )), this, SLOT( OnUploadProgress(qint64 ,qint64 )));
49 
50 }
51 
52 
53 void QKUploadFile::replyFinished(QNetworkReply *reply)
54 {
55     if(reply->error() == QNetworkReply::NoError)
56     {
57         qDebug()<<"no error.....";
58         QByteArray bytes = reply->readAll();  //获取字节
59         QString result(bytes);  //转化为字符串
60         qDebug()<<result;
61     }
62     else{
63         qDebug() << "replyFinished:  " << reply->error() << " " <<reply->errorString();
64     }
65 
66     QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
67     qDebug()<<status_code;
68 }
69 
70 void QKUploadFile::upLoadError(QNetworkReply::NetworkError errorCode)
71 {
72     qDebug() << "upLoadError  errorCode: " << (int)errorCode;
73 }
74 
75 void QKUploadFile::OnUploadProgress(qint64 bytesSent, qint64 bytesTotal)
76 {
77     qDebug() << "bytesSent: " << bytesSent << "  bytesTotal: "<< bytesTotal;
78 }

 

posted on 2014-06-24 10:12  小凡慢慢学  阅读(5883)  评论(0编辑  收藏  举报