如何从return语句中检索Google驱动器文件的绝对链接? [英] How to retrieve absolute link of google drive file from return statement?

查看:98
本文介绍了如何从return语句中检索Google驱动器文件的绝对链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html表单,我将一个媒体文件上传到Google驱动器并返回了文件. 这是我的代码:

I have a html form and Im uploading a media file to google drive and return the file. This is my code:

<form id="form">
  <input name="file" id="uploadfile" type="file">
  <input name="filename" id="filename" type="text">
  <input id="submit" type="submit">
</form>
<script>
const form = document.getElementById('form');
form.addEventListener('submit', e => {
  e.preventDefault();
  const file = form.file.files[0];
  const fr = new FileReader();
  fr.readAsArrayBuffer(file);
  fr.onload = f => {
    
    const url = "https://script.google.com/macros/s/###/exec";  // <--- Please set the URL of Web Apps.
    
    const qs = new URLSearchParams({filename: form.filename.value || file.name, mimeType: file.type});
    fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
    .then(res => res.json())
    .then(e => console.log("https://drive.google.com/uc?export=download&id=" + e.fileId))
    .catch(err => console.log(err));
  }
});
</script>

现在,当我上传example.mp3并在textinput字段中输入stackoverflow时,我会收到一个下载链接.我希望届时将要下载的文件为stackoverflow.mp3,但只有它的stackoverflow,文件类型结尾丢失.有人看到我在那里想念什么吗?

Now when I upload example.mp3 and type in textinput field stackoverflow, im receiving a downloadlink. I expect that the file which will get downloaded is stackoverflow.mp3 then, but its only stackoverflow, the filetype ending is missing. Does anybody see what im missing there?

无论如何,我要返回的是Google驱动器中文件的最终链接或直接链接. (我不知道这个词,对不起,我的英语不好)

Anyways, what I want to return is the final, or directlink of the file in google drive. (I dont know the word for it, sorry for my bad english)

如果您单击链接,我想在浏览器中打开文件,我希望您可以在webview中播放声音,就像在这里:

If you click the link, I want to open the file in browser, I want that you can play the sound in webview, like it is here: https://files.freemusicarchive.org/storage-freemusicarchive-org/music/Creative_Commons/Dead_Combo/CC_Affiliates_Mixtape_1/Dead_Combo_-_01_-_Povo_Que_Cas_Descalo.mp3

如何更改我的JavaScript以接收此类链接?

how to change my javascript to recieve this kind of link?

——————编辑————

————— EDIT ———

一个stackoverflow用户描述了如何获取我需要的链接.如何在脚本中放置此步骤?

One stackoverflow User described how to get the link I need. How to put this steps in the script?

我试图在Google Actions的SSML音频标签中完成此操作.上述步骤似乎都不起作用.我终于找到了解决方案.

I was trying to accomplish this inside the SSML audio tag for Actions on Google. None of the above steps seemed to work. I finally found a solution.

  1. 从共享链接 https:中获取文件ID: //drive.google.com/file/d/your_file_id/view?usp=sharing

构建直接链接 http://docs.google. com/uc?export = open& id = your_file_id

将直接链接粘贴到Web浏览器中,然后按Enter键

Paste the direct link into a web browser and hit enter

在浏览器重定向后复制结果URL注意:这将是一个更长的URL,看起来像这样:

Copy the resulting URL after you have been redirected by your browser Note: This will be a much longer URL that looks something like this: https://doc-XX-XX-docs.googleusercontent.com/docs/securesc/XXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXX/000000000000000/0000000000000000000000/*/your_file_id?e=open

使用此最终到达网址是我可以使上传的声音文件与Google上的Actions一起使用的唯一方法.

Using this final URL is the only way I could get my uploaded sound files to work with Actions on Google.

推荐答案

我认为文件类型保存在Google云端硬盘中.但是,当下载文件时,我认为由于没有扩展名,所以可能不知道文件类型.那么上传文件时添加扩展名又如何呢?

I think that the file type is kept on the Google Drive. But when the file is downloaded, I thought that the file type might be not known because of no extension. So how about adding the extension when the file is uploaded?

修改脚本后,它如下所示.

When your script is modified, it becomes as follows.

fr.onload = f => {
  
  const url = "https://script.google.com/macros/s/###/exec";  // <--- Please set the URL of Web Apps.
  
  const qs = new URLSearchParams({filename: form.filename.value || file.name, mimeType: file.type});
  fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
  .then(res => res.json())
  .then(e => console.log("https://drive.google.com/uc?export=download&id=" + e.fileId))
  .catch(err => console.log(err));
}

到:

fr.onload = f => {
  // I added below script.
  let newName = form.filename.value;
  const orgName = file.name;
  if (orgName.includes(".")) {
    const orgExt = orgName.split(".").pop();
    if (orgExt != newName.split(".").pop()) {
      newName = newName ? `${newName}.${orgExt}` : orgName;
    }
  }
  
  const url = "https://script.google.com/macros/s/###/exec";
  
  const qs = new URLSearchParams({filename: newName, mimeType: file.type});  // Modified
  fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
  .then(res => res.json())
  .then(e => console.log(e.fileUrl))  // <--- You can retrieve the returned value here.
  .catch(err => console.log(err));
}

这篇关于如何从return语句中检索Google驱动器文件的绝对链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆