使用javascript读取文件并将其发送到服务器(Read file with javascript and send it to server)

编程入门 行业动态 更新时间:2024-10-25 23:34:59
使用javascript读取文件并将其发送到服务器(Read file with javascript and send it to server)

我使用谷歌驱动器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 resource

I 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.

更多推荐

本文发布于:2023-08-03 17:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1395148.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   服务器   文件   并将其   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!