【问题标题】:Is it possible to use client-side generated blob url to save to Google Drive是否可以使用客户端生成的 blob url 保存到 Google Drive
【发布时间】:2014-03-21 20:36:53
【问题描述】:

我正在生成一个 CSV 客户端并将其放入 Blob,然后创建一个对象 URL。

我试图完成的是生成这个 blob URL,然后将文件保存到 Google Drive。我在这个例子中使用了保存到驱动器按钮,但它似乎甚至没有加载保存按钮,除非我去掉“blob:http:”,在这种情况下它会看起来正确加载按钮,但这不是一个有效的文件了。

这甚至可以将 blob 文件保存到 Google 云端硬盘吗?

我的代码如下所示:

    var data = [["one", "info 1", "additional 1"], ["two", "info 2", "additional 2"]],
        csvContent = [], output, objectURL;

    data.forEach(function(infoArray, index) {
        var dataString = infoArray.join(",");
        csvContent += index < infoArray.length ? dataString+ "\n" : dataString;
    });

    output = new Blob([csvContent], { type: 'text/csv' });
    objectURL = URL.createObjectURL(output);

    gapi.savetodrive.render('savetodrive-div', {
      src: objectURL,
      filename: 'save-to-drive.csv',
      sitename: 'Example'
    });

谢谢!

【问题讨论】:

  • 我希望能够做到这一点。我可以用 Dropbox 做到这一点。

标签: javascript html google-drive-realtime-api bloburls


【解决方案1】:
var data = [["one", "info 1", "additional 1"], ["two", "info 2", "additional 2"]],
    csvContent = [], output, objectURL;

data.forEach(function(infoArray, index) {
    var dataString = infoArray.join(",");
    csvContent += index < infoArray.length ? dataString+ "\n" : dataString;
});

output = new Blob([csvContent], { type: 'text/csv' });
objectURL = URL.createObjectURL(output);

gapi.savetodrive.render('savetodrive-div', {
  src: objectURL,
  filename: 'save-to-drive.csv',
  sitename: 'Example'
});

这是你需要的吗? 信用:http://qaru.site/questions/4397835/is-it-possible-to-use-client-side-generated-blob-url-to-save-to-google-drive

【讨论】:

    【解决方案2】:

    【讨论】:

    【解决方案3】:

    它不适用于 'blob:' URI,请参阅下面的详细信息。

    This URL about data URIs 没有特别提到blob:,也没有提到does this reference about the save to google drive button。因此,我必须自己尝试使用 blobl: URI 来确定。

    this fiddle

    下面的 sn-p 是相同的,只是它会导致错误提示“文档已被沙盒化并且缺少 'allow-same-origin' 标志。”当我尝试它时。 (因此我包含了小提琴 URL。)

    $(document).ready(function() {
    	var blob = new Blob(["test"], { type: "text/plain" })
      var url = window.URL.createObjectURL(blob);
    	const attributeName = "data-src";
      $("#but").attr(attributeName, url);
      console.log(attributeName + ": " + $("#but").attr(attributeName));
      $.getScript("https://apis.google.com/js/platform.js");
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="but" class="g-savetodrive" data-src="/test.pdf" data-filename="test.txt" data-sitename="TryItOut.Inc">
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多