Uploading big files with POST freezes the system

440 views Asked by At

i have an application which upload files (video files) to a server using the standard post request, something like:

QNetworkRequest  request(QUrl("myUrl"));
QString bound="margin";
QByteArray data(QString("--"+bound+"\r\n").toLatin1());

data += "Content-Disposition: form-data; name=\"media_id\"\r\n\r\n";
data += id + "\r\n";
data += QString("--" + bound + "\r\n").toLatin1();

QString str = "Content-Disposition: form-data; name=\"file\"; filename=\"" + name + "\r\n";
data += str;
data += "Content-Type: video/mp4\r\n\r\n";

QFile f(path);
if(! f.open(QIODevice::ReadOnly))
{
    qDebug() << "uploadMediaRequest error opening " << path;
    return false;
}


QByteArray bytes;
bytes = f.readAll();

data.append(bytes);
data += "\r\n";
data += QString("--" + bound + "\r\n.").toLatin1();
data += "\r\n";

f.close();

request.setRawHeader(QString("Accept").toLatin1(),QString("application/json, text/javascript, */*; q=0.01").toLatin1());
request.setRawHeader(QString("Origin").toLatin1(),QString("http://dev.teamhood.io").toLatin1());
request.setRawHeader(QString("API-Client").toLatin1(),QString("desktop").toLatin1());
request.setRawHeader(QString("withCredentials").toLatin1(), QString("true").toLatin1());
request.setRawHeader(QString("Content-Type").toLatin1(),QString("multipart/form-data; boundary=" + bound).toLatin1());
request.setRawHeader(QString("Content-Length").toLatin1(), QString::number(data.length()).toLatin1());

And then POST that request. For files of 1GB or less it works just fine but if i try to upload a file of 1,5GB i get

myProgramName[948:32028] Communications error: <OS_xpc_error: <error: 0x7fff7c37bb60> { count = 1, contents =
"XPCErrorDescription" => <string: 0x7fff7c37bfa8> { length = 22, contents = "Connection interrupted" }}>
Got xpc error message: Connection interrupted

and the app and the SO freeze to death. I suppose i can split the big files and upload them in parts and make the server assemble them but i was wondering if any of you could give me an explanation for this strange errors.

I work with OSX & Qt 5.4, thx in advance!

1

There are 1 answers

0
Sommerwild On BEST ANSWER

Solved by user Pavel Strakhov:

QNetworkRequest request(QUrl("myUrl"));

QFile *f = new QFile(path);
if(! f->open(QIODevice::ReadOnly))
{
    qDebug() << "uploadMediaRequest error opening " << path;
    return false;
}

QHttpMultiPart * multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart idPart, mediaPart;
idPart.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"media_id\""));
idPart.setBody(id.toLatin1());

mediaPart.setHeader(QNetworkRequest::ContentTypeHeader, type); 
mediaPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\""+ name + "\""));
mediaPart.setBodyDevice(f);
f->setParent(multiPart); 

multiPart->append(idPart);
multiPart->append(mediaPart);

request.setRawHeader(QString("Accept").toLatin1(),QString("application/json, text/javascript, */*; q=0.01").toLatin1());
request.setRawHeader(QString("Origin").toLatin1(),QString("http://dev.teamhood.io").toLatin1());
request.setRawHeader(QString("API-Client").toLatin1(),QString("desktop").toLatin1());
request.setRawHeader(QString("withCredentials").toLatin1(), QString("true").toLatin1());

networkManager->post(request, multiPart));

Now it runs great.