Uploading a file in Multipart form data in Qt5



  • Any one can help me please, how to upload picture in Multipart form data in QT??





  • I try it ,but it didn't work I got this error:
    QIODevice::read (QFile, "file:///home/Images/img.png"): device not open



  • @jiji

    Can you share your code how you try to add the image as multi part?



  • void product::add(QString photos)
    {
    QString id=settings.value("id").toString();
    qDebug() <<"id"<<id;

    QString token=settings.value("token").toString();
    qDebug() <<"token"<<token;
    
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    
    QHttpPart photo;
    photo.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/*"));
    photo.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"photo\""));
    QFile *file = new QFile(photos);
    file->open(QIODevice::ReadWrite);
    photo.setBodyDevice(file);
    file->setParent(multiPart);
    
    multiPart->append(photo);
    
    QUrl url("http://localhost:3070/auth/v1/product/Add");
    QNetworkRequest request(url);
    request.setRawHeader("x-access-token","" + token.toUtf8());
    QNetworkAccessManager manager;
    QNetworkReply *reply = manager.post(request, multiPart);
    multiPart->setParent(reply);
    

    while(!reply->isFinished())
    { qApp->processEvents(); }
    }


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.