Google Drive SDK Javascript恢复上传示例

时间:2014-03-27 11:35:34

标签: javascript file-upload google-drive-api

我可以使用FileReader使用Javascript上传文件,并使用gapi.client.request insert = all good。

我可以请求刷新令牌=一切都好。

我可以发起简历上传请求并检索简历上传ID =全部好。

当我使用我的简历上传ID发送文件的第一个CHUNK时,我收到了#34; Bad Request"。

发送我尝试过的请求: (a)gapi.client.request (b)XMLHttpRequest

我按照https://developers.google.com/drive/web/manage-uploads#resumable

形成了请求

我尝试了各种内容类型选项。

我已经使用Fiddler确认了有效负载,并根据上面的简历链接确认标题和请求看起来是正确的。

我已使用以下两种方法将二进制文件添加到正文中: (a)base64Data (b)Uint8Array

其中几个例子是:

            var base64Data = arrayBufferToBase64(_buffer, _start, _packetLen);
            var request = gapi.client.request({
            'path': _resumeUploadLoc,
            'method': 'PUT',
            'headers': {
                'Content-Type': 'application/octet-stream',
                'Content-Length': _packetLen,
                'Content-Range': _rangeBottom + '-' + _rangeTop + '/' + _len
            },
            'body': base64Data
        });
        request.execute(UploadChunkComplete);        

httpRequest示例是:

        function UploadFileChunks() {
        var base64Data = arrayBufferToBase64(_buffer, _start, _packetLen);
        var xhr = new XMLHttpRequest();
        xhr.open("PUT", _resumeUploadLoc, false);
        xhr.setRequestHeader("Authorization", "Bearer " + _cat);
        xhr.setRequestHeader("Content-Length", _packetLen);
        xhr.setRequestHeader("Content-Type", 'application/octet-stream');
        xhr.setRequestHeader("Content-Range", _rangeBottom + '-' + _rangeTop + '/' + _len);
        xhr.onreadystatechange = function(xmlhttp, m) {
            debugger;
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var r = xmlhttp.responseText;
            }
        };
        try {
            xhr.send(base64Data);
        }
        catch (Error) {
            debugger;
            var x = xhr.responseText;
            alert(Error + ' ' + x);
        }
    }

所有返回错误请求。

有人可以帮我解决这个难题的最后一部分。我需要使用简历上传协议上传文件块的javascript示例。

非常感谢任何帮助。

请求

以下请求是使用Google Drive API gapi.client.request方法生成的。

PUT https://content.googleapis.com/upload/drive/v2/files?uploadType=resumable&upload_id=AEnB2Urju4toOENQjIMvqZcajW3_QNHtXtZq4zRLAwRqUZBXrNnEjtSjl5aHz5iMwL5ilB30Ut66NTXGEZkT5bMmJKOmFQy06g HTTP/1.1

Host: content.googleapis.com
Connection: keep-alive
Content-Length: 699052
Authorization: Bearer ya29.1.AADtN_VESMwfL-BgTvtDn64qt49Yd7bw9SWZfIRCIpP6OU9tAnIDPZl9QJARciSViA
Origin: https://content.googleapis.com
X-Origin: http://lh:2377
X-ClientDetails: appVersion=5.0%20(Windows%20NT%206.1%3B%20WOW64)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F33.0.1750.154%20Safari%2F537.36&platform=Win32&userAgent=Mozilla%2F5.0%20(Windows%20NT%206.1%3B%20WOW64)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F33.0.1750.154%20Safari%2F537.36

X-Goog-Encode-Response-If-Executable: base64
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Content-Type: application/octet-stream
Content-Range: 0-524287/2006733
X-JavaScript-User-Agent: google-api-javascript-client/1.1.0-beta
X-Referer: http://lh:2377
Accept: */*
Referer: https://content.googleapis.com/static/proxy.html?jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.-GPDtONAWXU.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Fz%3Dzcms%2Frs%3DAItRSTM9lxlkBNnz0jxjBbPofc0LMmjoNw

Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

/9j/4AAQSkZJRgABAQEASABIAAD/4SMQRXhpZgAATU0AKgAAAAgACwEPAAIAAAAGAAAIngEQAAIAAAAJAAAIpAESAAMAAAABAAEAAAEaAAUAAAABAAAIrgEbAAUAAAAB

RESPONSE

HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 193
Date: Fri, 28 Mar 2014 00:30:16 GMT
Server: HTTP Upload Server Built on Mar 20 2014 15:26:37 (1395354397)
Alternate-Protocol: 443:quic

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "badRequest",
    "message": "Invalid Upload Request"
   }
  ],
  "code": 400,
  "message": "Invalid Upload Request"
 }
}

0 个答案:

没有答案