为什么上传到谷歌硬盘时没有设置文件名?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么上传到谷歌硬盘时没有设置文件名?相关的知识,希望对你有一定的参考价值。

我的目标 我想上传一个sqlite文件到谷歌驱动器。

虽然我已经能够将文件上传到谷歌驱动器,但文件上传的名称是“无标题”。我在Qt的Performing Multipart Upload的帮助下尝试了QHttpMultiPart

这是我的代码

bool File_Control::Upload_File_Multipart(const QString &FileName, const QByteArray &rawData)
{
    if(FileName.isEmpty()){
        emit setMessage("Error: File Name can not be empty");
        return false;
    }

    if(rawData.size()==0){
        emit setMessage("Error: File size can not be zero");
        return false;
    }

    // Prepare MetaDataPart
    QHttpPart MetadataPart;
    MetadataPart.setRawHeader("Content-Type", "application/json; charset=UTF-8");
    QString Body;
    Body = "{
"
            + tr(""name": "%1"
").arg(FileName)
            + tr("}");
    MetadataPart.setBody(Body.toUtf8());

    // Prepare MediaPart 
    QHttpPart MediaPart;
    MediaPart.setRawHeader("Content-Type", "application/octet-stream");
    MediaPart.setBody(rawData);

    // Now add MetaDataPart and MediaPart together to form multiPart
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::RelatedType);
    multiPart->setBoundary("bound1"); 
    multiPart->append(MetadataPart);
    multiPart->append(MediaPart);

    // Now Prepare the request
    QUrl url("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart");
    QNetworkRequest request(url);
    QString AccessTokenBearer = "Bearer " + mAccess_Token; // mAccess_Token holds the access_token for the session
    request.setRawHeader("Content-Type","Content-Type: multipart/related; boundary=bound1");
    request.setRawHeader("Authorization", AccessTokenBearer.toUtf8());

    // Get Reply and connect it to set Value
    QNetworkReply *reply = mQNAM.post(request, multiPart); // mQNAM is QNetworkAccessManager
    QObject::connect(reply, SIGNAL(uploadProgress(qint64,qint64)),
                     this, SIGNAL(setValue(qint64,qint64))); // Update in gui

    // Set timeout mechanism while waiting for reply finished
    bool stop = false;
    QTimer timer;
    timer.setSingleShot(true);
    QObject::connect(reply, &QNetworkReply::uploadProgress, [&](){
        timer.stop();
        timer.start(TIMEOUT); // Restart timer every time upload progress
    });
    QObject::connect(&timer, &QTimer::timeout, [&](){
        stop = true;
    });
    timer.start(TIMEOUT);

    // Wait till the response is completed
    while(!reply->isFinished()){
        QCoreApplication::processEvents();
        if(stop){
            reply->abort();
        }
    }

    // Check reply
    if(reply->error() != QNetworkReply::NoError){
        if(reply->error() == QNetworkReply::OperationCanceledError){
            emit setMessage("Error: Timeout");
        }
        else{
            emit setMessage("Error: "+reply->errorString());
        }
        delete reply;
        return false;
    }
    else{
        QByteArray rawData = reply->readAll();
        qDebug()<<"Size of reply "<<rawData.size();
        qDebug()<<"Data "<<rawData;
        delete reply;
        return true;
    }
}  

编辑1 Body.utf8 log - >“{ n ”name “:”db_06_08_2018_16_18_11_979.sqlite “ n}”

答案

我终于找到了错误。这只是准备请求时的错字 之前:

 request.setRawHeader("Content-Type","Content-Type: multipart/related; boundary=bound1");

后:

 request.setRawHeader("Content-Type","multipart/related; boundary=bound1");  

这解决了这个问题。

以上是关于为什么上传到谷歌硬盘时没有设置文件名?的主要内容,如果未能解决你的问题,请参考以下文章

FTP 到谷歌存储

我想只将更新的文件上传到谷歌驱动器,但XCOPY似乎无法正常工作

GPG服务测试没有上传新的apk签署到谷歌播放

有没有哪位大神能把Windows系统上传到谷歌云盘的?

从树莓派上传文件到谷歌驱动器

我已经制作了一个使用谷歌地图的应用程序我现在可以加载到谷歌播放而不破坏版权?