如何从返回语句中检索谷歌驱动文件的绝对链接



我有一个html表单,我正在上传一个媒体文件到谷歌驱动器并返回该文件。这是我的代码:

<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并在文本输入字段stackoverflow中键入时,我收到了一个下载链接。我希望下载的文件是stackoverflow.mp3,但它只是stackoverflow,缺少文件类型结尾。有人看到我在那里丢了什么吗?

无论如何,我想返回的是谷歌驱动器中文件的最终链接。(我不知道该怎么说,对不起我英语不好(

如果你点击链接,我想在浏览器中打开文件,我想你可以在webview中播放声音,就像这里一样: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以接收这种链接?

----------编辑------

一位stackoverflow用户描述了如何获得我需要的链接。如何将这些步骤放入脚本中?

我试图在谷歌Actions的SSML音频标签中实现这一点。上述步骤似乎都不起作用。我终于找到了解决办法。

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

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

  3. 将直接链接粘贴到web浏览器中,然后点击回车

  4. 在浏览器重定向后复制生成的URL注意:这将是一个更长的URL,看起来像这样:https://doc-XX-XX-docs.googleusercontent.com/docs/securesc/XXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXX/000000000000000/0000000000000000000000/*/你的_file_id?e=打开

使用这个最终URL是我在谷歌上使用Actions获得上传的声音文件的唯一方法。

我认为文件类型保留在Google Drive上。但是当下载文件时,我认为文件类型可能不知道,因为没有扩展名。那么,在上传文件时添加扩展名怎么样?

当您的脚本被修改时,它变成如下。

修改的脚本:

发件人:
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));
}

最新更新