2016-07-04 9 views
0

Multipartのサーバーにファイルをアップロードする必要があります。私はQtのdocsとほとんど同じコードを持っていますが、ファイルはサーバにアップロードされていません。Qt5でマルチパート形式のファイルをアップロードする

は、ここで私は私のデバッグ中に持っているものです。

---------Uploaded-------------- 3672 of 3672 

---------Uploaded-------------- 3672 of 3672 

---------Uploaded-------------- 3672 of 3672 

---------Uploaded-------------- 0 of 0 

----------Finished-------------- 

"Error transferring http://MyUrlHere.com/uploadFile - server replied: Bad Request" 400 QNetworkReplyHttpImpl(0x17589ff0) 

私はクロームやFirefoxの拡張子とマルチパートでその上にファイルをアップロードしようとすると、それが実際に動作するため、問題がサーバから来ていません!ここで

は私のコードです:

 QUrl testUrl("http://MyUrlHere.com/uploadFile "); 
    QNetworkRequest request(testUrl); 
    QNetworkProxy proxy; 

    proxy.setType(QNetworkProxy::HttpProxy); 
    proxy.setHostName("proxy"); 
    proxy.setPort(8080); 
    QNetworkProxy::setApplicationProxy(proxy); 

    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); 

    QString preview_path = "C:/Users/Desktop/image.jpg"; 
    QHttpPart previewPathPart; 
    previewPathPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_path\"")); 
    previewPathPart.setBody(preview_path.toLatin1()); 

    QString preview_name = "image.jpg"; 

    QHttpPart previewFilePart; 
    previewFilePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg")); 
    previewFilePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"preview_file\"; filename=\""+ preview_name + "\"")); 
    QFile *file = new QFile(preview_path); 

    file->open(QIODevice::ReadOnly); 
    previewFilePart.setBodyDevice(file); 
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart 

    multiPart->append(previewPathPart); 
    multiPart->append(previewFilePart); 

    QNetworkAccessManager *networkManager= new QNetworkAccessManager; 
    reply = networkManager->post(request, multiPart); 
    multiPart->setParent(reply); // delete the multiPart with the reply 

    connect(reply, SIGNAL(finished()), 
       this, SLOT (uploadDone())); 

    connect(reply, SIGNAL(uploadProgress(qint64, qint64)), 
       this, SLOT (uploadProgress(qint64, qint64))); 

} 


void ApkDialog::uploadProgress(qint64 bytesSent, qint64 bytesTotal) { 
    qDebug() << "---------Uploaded--------------" << bytesSent<< "of" <<bytesTotal; 
} 

void ApkDialog::uploadDone() { 
    qDebug() << "----------Finished--------------" << reply->errorString() <<reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); 
    qDebug()<<reply; 

    // reply->deleteLater(); 

} 
+0

まず、インドのスタイルで書き込みを停止、あなたが行うすべてをチェック。たとえば、成功を確認せずにファイルを開くのは普通ではありません。あなたの質問については、コードですべて正しいです。ヘルプが必要な場合は、APIメソッドのドキュメントを提供する必要があります。 – UndeadDragon

+0

ありがとうございます@UndeadDragonあなたの答えです。マルチパートリクエストには、関連するコネクタで設定されたmaxPostSizeの制限を超えたパラメータデータ(アップロードされたファイルを除く)が含まれていました。私は実際にサイズを10Mb(これは非常に大きい)に固定しました。私のAPIは単純なもので、名前とファイルだけを要求します。 ChromeのPostManエクステンションでテストしたところ、完璧に動作しています! – Chakib

+1

これは間違いなくQtエラーではありませんが、あなたの要求には何か悪いです。 APIメソッドのマニュアルがなければ答えにくいです。 – UndeadDragon

答えて

3

私はエラーを検出しました。リクエストエラーでした。 Qtのドキュメントには欠けているものがあります。ここで

が働いている私のコードです:すべての

QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); 
    QHttpPart imagePart; 
    //imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); 
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"version.txt\""));/* version.tkt is the name on my Disk of the file that I want to upload */ 

    QHttpPart textPart; 
    textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"name\"")); 
    textPart.setBody("toto");/* toto is the name I give to my file in the server */ 

    QString apkLocation = apktextEdit->text(); 
    QFile *file = new QFile(apkLocation); 
    file->open(QIODevice::ReadOnly); 
    imagePart.setBodyDevice(file); 
    file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart 

    multiPart->append(textPart); 
    multiPart->append(imagePart); 

    QUrl url("http://MyUrl.com"); 
    QNetworkRequest request(url); 

    QNetworkAccessManager *networkManager= new QNetworkAccessManager; 
    reply = networkManager->post(request, multiPart); 
    multiPart->setParent(reply); // delete the multiPart with the reply 

    connect(reply, SIGNAL(finished()), 
       this, SLOT (uploadDone())); 

    connect(reply, SIGNAL(uploadProgress(qint64, qint64)), 
       this, SLOT (uploadProgress(qint64, qint64))); 
} 
関連する問題