通过网页

时间:2016-01-08 10:07:40

标签: javascript google-app-engine google-cloud-storage

我使用Google提供的文档here尝试将图片从网页上传到Google云端存储。我认为存储设置正确,我认为我已经得到了正确的值。

使用时,图像会被识别,但由于某种原因,图片无法上传。我不确定它是代码还是环境的设置方式。此外,没有报告错误消息。我知道图像是通过事件进入功能的。

javascript是......

        /**
         * Google Cloud Storage API request to insert an object into
         * your Google Cloud Storage bucket.
         */
        function insertObject(event) {
            try {
                //var MyFile = document.getElementById("take-picture").files;
                var fileData = event.target.files[0];
                //var fileData = document.getElementById("take-picture").files;
            } 
            catch(e) {
                //'Insert Object' selected from the API Commands select list
                //Display insert object button and then exit function
                //filePicker.style.display = 'block';
                return;
            }
          var boundary = '-------314159265358979323846';
          var delimiter = "\r\n--" + boundary + "\r\n";
          var close_delim = "\r\n--" + boundary + "--";

            var reader = new FileReader();
            reader.readAsBinaryString(fileData);
            reader.onload = function(e) {
                var contentType = fileData.type || 'application/octet-stream';
                var metadata = {
                    'name': fileData.name,
                    'mimeType': contentType
                };

                var base64Data = btoa(reader.result);
                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;

                //Note: gapi.client.storage.objects.insert() can only insert
                //small objects (under 64k) so to support larger file sizes
                //we're using the generic HTTP request method gapi.client.request()
                var request = gapi.client.request({
                    'path': '/upload/storage/' + API_VERSION + '/b/' + BUCKET + '/o',
                    'method': 'POST',
                    'params': {'uploadType': 'multipart'},
                    'headers': {
                        'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
                    },
                    'body': multipartRequestBody});
                //Remove the current API result entry in the main-content div
                //listChildren = document.getElementById('main-content').childNodes;
                //if (listChildren.length > 1) {
                //    listChildren[1].parentNode.removeChild(listChildren[1]);
                //} 

                //look at http://stackoverflow.com/questions/30317797/uploading-additional-metadata-as-part-of-file-upload-request-to-google-cloud-sto

                try{
                    //Execute the insert object request
                    executeRequest(request, 'insertObject');
                    //Store the name of the inserted object 
                    object = fileData.name;         
                }
                catch(e) {
                    alert('An error has occurred: ' + e.message);
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

不太确定问题出在哪里。

但在我的工作代码中,我正在使用

var metadata = {
    'title': fileData.fileName || fileData.name,
    'parents': [{
    "kind": "drive#fileLink",
            "id": folderId
    }],
    'mimeType': contentType
};

因此,您的代码可能会丢失文件夹的ID

API请求:

var request = gapi.client.request({
        'path': '/upload/drive/v2/files',
        'method': 'POST',
        'params': {'uploadType': 'multipart'},
        'headers': {
          'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
        },
        'body': multipartRequestBody
});

FYR。