Google云端硬盘可以上传文件,但无法查看

皇家爸爸

以下代码可用于将PDF文件(使用jsPDF创建)加载到我的Google云端硬盘文件夹中。本质上,它是Google云端硬盘的快速入门代码已修改。

在用“ docMenu.output('dataurlnewwindow');”打开的浏览器中查看时,生成的PDF看起来不错。

PDF文件在我的Google云端硬盘文件夹中显示为OK,但是当我查看该文件时,出现灰色屏幕,显示“无法加载PDF文档”。我在控制台/检查中没有执行错误。

我错过了什么?我是否通过拉动Blob(数据URI)并将其提供给Google云端硬盘上传而搞砸了?

非常感谢!

   <script type="text/javascript">
      var CLIENT_ID = 'my-client-id';
      var SCOPES = 'https://www.googleapis.com/auth/drive';
      var FOLDER_ID = 'my-folder-id';

      /**
       * Called when the client library is loaded to start the auth flow.
       */
      function handleClientLoad() {
        window.setTimeout(checkAuth, 1);
      }

      /**
       * Check if the current user has authorized the application.
       */
      function checkAuth() {
        gapi.auth.authorize(
            {'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true},
            handleAuthResult);
      }

      /**
       * Called when authorization server replies.
       * @param {Object} authResult Authorization result.
       */
      function handleAuthResult(authResult) {
        if (authResult && !authResult.error) {
          // Access token has been successfully retrieved, requests can be sent to the API.

           var docMenu = new jsPDF('p', 'cm', 'a5');   <-- Create PDF document
           docMenu.text(2, 1, "Testing");           <-- Add text here...
           docMenu.text(2, 5, "Testing");
           docMenu.text(2, 10, "Testing");
           docMenu.text(2, 15, "Testing");
           docMenu.text(2, 20, "Please order and pay at the counter.");
           docMenu.output('dataurlnewwindow');    <-- Displays PDF perfectly

           var MyBlob = docMenu.output('blob');     <-- get PDF Blob
           MyBlob.name = "test.pdf";                <-- Give it a file name

           insertFile(MyBlob);         <-- send it to Google Drive upload routine

        } else {
        alert("FAILED AUTHORIZING");        
        }
      }

      /**
       * Start the file upload.
       * @param {Object} evt Arguments from the file selector.
       */
      function uploadFile(evt) {
        gapi.client.load('drive', 'v2', function() {
          var file = evt.target.files[0];
          insertFile(file);
        });
      }

      /**
       * Insert new file.
       * @param {File} fileData File object to read data from.
       * @param {Function} callback Function to call when the request is complete.
       */
      function insertFile(fileData, callback) {
        const boundary = '-------314159265358979323846';
        const delimiter = "\r\n--" + boundary + "\r\n";
        const 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 = {
            'title': fileData.name,
            'mimeType': contentType,
            'parents': [{"id":FOLDER_ID}]
          };

          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;

          var request = gapi.client.request({
              'path': '/upload/drive/v2/files/',
              'method': 'POST',
              'params': {'uploadType': 'multipart'},
              'headers': {
                'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
              },
              'body': multipartRequestBody});
          if (!callback) {
            callback = function(file) {
              console.log(file)
            };
          }
          request.execute(callback);
        }
      }
    </script>
皇家爸爸

对于可能会有所帮助的人,上面的代码非常适合我使用jsPDF创建PDF文件并将其上传到Google云端硬盘。

我面临的问题是云端硬盘出于某种原因不喜欢该PDF的问题之一(因为论坛中的其他PDF都报告了该问题)。

该文件可以下载,并且可以在Acrobat,Foxit和其他应用程序中完美打开。问题出在Google。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Google 云端硬盘上传文件

将文件上传到Google云端硬盘?

查看上传到Google云端硬盘的图像

无法将文件上传到Google云端硬盘中的文件夹

无法将文件上传到Google云端硬盘上的特定文件夹

无法使用Google App脚本上传到Google云端硬盘上的子文件夹

Google云端硬盘-无法同步的文件

无法使用API在Google云端硬盘中打开上传的文件

Google云端硬盘:删除1天以上的文件夹

可以取消Google云端硬盘上传吗?

无法在js中的Google云端硬盘上上传图片

使用gapi将文件上传到Google云端硬盘

C#Google云端硬盘APIv3上传文件

使用php curl将文件上传到Google云端硬盘

在后台将文件上传到Google云端硬盘

Google云端硬盘文件上传已转换为文档

在Google云端硬盘中找不到上传的文件

使用Dropzone将文件上传到Google云端硬盘

将文件从网络上传到Google云端硬盘

使用http上传到Google云端硬盘文件夹

将文件从Blobstore上传到Google云端硬盘

使用意图将文件上传到Google云端硬盘

有没有一种方法可以“刷新” Google云端硬盘文件查看器?

是否可以获取Google云端硬盘文件的永久链接?

DrEdit-测试应用程序:部署成功,但无法创建文本文件,无法看到用户的Google云端硬盘

无法通过其ID从Google云端硬盘下载文件

无法共享文件(通过Gmail或Google云端硬盘)

无法自动永久删除Google云端硬盘上的文件

使用Google Apps脚本从Google云端硬盘中的多个上传元素上传本地文件