【问题标题】:Success upload to Google Drive but empty file when open (Still have size)成功上传到 Google Drive 但打开时为空文件(仍有大小)
【发布时间】:2018-10-16 10:45:06
【问题描述】:

将图片 URL 转换为 base64 字符串后,我尝试将此 base64 图片上传到 Goolge Drive,一切正常:响应代码 200 和 Goole Drive 在 App/Web 中显示此文件。

但是当我打开这张图片时,它就像谷歌在base64字符串上改变了一些东西,所以它无法读取。

下载图像从 GD 转换为 base64 字符串后,我看到我原来的 base64 字符串发生了变化。

上传前:https://anotepad.com/notes/nt6fct

来自 Google 云端硬盘的文件:https://anotepad.com/notes/ayg57q

上传代码:

testUploadFile(fileInfo, base64Data){
    const boundary = '-------314159265358979323846';
    const delimiter = "\r\n--" + boundary + "\r\n";
    const close_delim = "\r\n--" + boundary + "--";
    var contentType = fileInfo.type || 'application/octet-stream';
    var metadata = {
        'name': fileInfo.title,
        'mimeType': contentType
    };

    var multipartRequestBody =
        delimiter +
        'Content-Type: application/json\r\n\r\n' +
        JSON.stringify(metadata) +
        delimiter +
        'Content-Type: ' + contentType + '\r\n' +
        'Content-Transfer-Encoding: base64\r\n' +
        '\r\n' +
        base64Data +
        close_delim;

    gapi.client.request({
        'path': '/upload/drive/v3/files',
        'method': 'POST',
        'params': {
            'uploadType': 'multipart'
        },
        'headers': {
            'Content-Type': 'multipart/related; boundary="' + boundary + '"'
        },
        'body': multipartRequestBody
    }).then(function(response){
        console.log(response);
    });
}

出了点问题@@你能纠正一下吗? 提前致谢!

【问题讨论】:

    标签: javascript google-api google-drive-api base64 google-api-js-client


    【解决方案1】:

    这个简单的示例可能会帮助您找到代码的问题。

    function createFile(){
    const boundary = '-------314159265358979323846';
    const delimiter = "\r\n--" + boundary + "\r\n";
    const close_delim = "\r\n--" + boundary + "--";
    
    var fileContent = 'It works :)';
    
    var metadata = {
        'name': 'myFile',
        'mimeType': 'text/plain\r\n\r\n'
    };
    
    var multipartRequestBody = delimiter +  'Content-Type: application/json\r\n\r\n' + JSON.stringify(metadata) + delimiter + 'Content-Type: ' + 'text/plain\r\n\r\n' + fileContent + close_delim;
    
    gapi.client.request({
        'path': '/upload/drive/v3/files',
        'method': 'POST',
        'params': {
            'uploadType': 'multipart'
        },
        'headers': {
            'Content-Type': 'multipart/related; boundary="' + boundary + '"'
        },
        'body': multipartRequestBody
    }).then(function(response){
        console.log(response);
    });
    }
    

    【讨论】:

    • 感谢 DalmTo :D 我忘了 + '\r\n\r\n'。
    猜你喜欢
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2018-08-06
    • 1970-01-01
    相关资源
    最近更新 更多