我使用谷歌驱动器API,我试图从网址下载一个文件,将其设置为变量并将其发送到后端。 请注意,我使用Google云端硬盘的JavaScript选择器。
旁注: 首先我尝试将id_token和fileId发送到后端并尝试访问该文件,但我无法使用相关作用域对用户进行身份验证,因为用户在javascript而不是php中进行了身份验证。
所以我决定用axios获取文件并将其保存到变量发送文件到我的后端。
var downloadUrl = typeof file.exportLinks !== 'undefined' ? file.exportLinks['application/pdf'] : file.webContentLink axios.get(downloadUrl, { headers: { Authorization: 'bearer ' + gapi.client.getToken().access_token, 'Access-Control-Allow-Origin': '*' } }).then(function (response) { console.log(response) }).catch(function (error) { console.log(error); });我试过用这个,但这个抛出
无法加载https://drive.google.com/uc?id=aba123bab23r24-m9_Mp4aiDcnibNf&export=download :对预检请求的响应未通过访问控制检查:没有'Access-Control-Allow-Origin'标头出现在请求的资源。 原因' http://deftfax.app '因此不被允许访问。
使用axios,我如何从url下载文件并将其设置为变量?
I am using google drive api and I am trying the download a file from url, set it to variable and send it to backend. Please note that I use javascript picker of Google Drive.
Sidenote: First I tried sending id_token and fileId to backend and tried accessing the file, but I couldn't authenticate the user with related scopes as user authenticated in the javascript not php.
So I decided to get the file with axios and save it to a variable send post the file to my backend.
var downloadUrl = typeof file.exportLinks !== 'undefined' ? file.exportLinks['application/pdf'] : file.webContentLink axios.get(downloadUrl, { headers: { Authorization: 'bearer ' + gapi.client.getToken().access_token, 'Access-Control-Allow-Origin': '*' } }).then(function (response) { console.log(response) }).catch(function (error) { console.log(error); });I tried using this, but this throws
Failed to load https://drive.google.com/uc?id=aba123bab23r24-m9_Mp4aiDcnibNf&export=download: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://deftfax.app' is therefore not allowed access.
Using axios, how can I download the file from url and set it to a variable?
最满意答案
下载文件中已经记录了使用Drive API下载文件的支持操作:
根据您要执行的下载类型 - 文件,Google文档或内容链接 - 您将使用以下网址之一:
使用alt =媒体文件资源下载文件 - files.get 下载并导出Google Doc - files.export 将用户链接到文件 - 文件资源的webContentLink我自己可以通过使用webcontentlink使用Picker API下载图像文件:
function downloadImage(data) { if (data.action == google.picker.Action.PICKED) { var fileId = data.docs[0].id; var webcontentlink = ' https://docs.google.com/uc?id='+fileId+'&export=download' window.open( webcontentlink,'image/png'); } }希望得到你的实施开始。
The supported operations for downloading files using Drive API is already documented in Download Files:
Depending on the type of download you'd like to perform — a file, a Google Document, or a content link — you'll use one of the following URLs:
Download a file — files.get with alt=media file resource Download and export a Google Doc — files.export Link a user to a file — webContentLink from the file resourceI myself can download an image file using Picker API by using the webcontentlink:
function downloadImage(data) { if (data.action == google.picker.Action.PICKED) { var fileId = data.docs[0].id; var webcontentlink = ' https://docs.google.com/uc?id='+fileId+'&export=download' window.open( webcontentlink,'image/png'); } }Hope that get's you started with your implementation.
更多推荐
发布评论