简体   繁体   English

文件是直接用Javascript还是通过网站下载?

[英]Are files downloaded directly in Javascript or through the website?

When I want to download a file by making the XmlHttpRequest in Javascript from a seperate server, will the file be downloaded through the website hosting the Javascript page? 当我想通过从单独的服务器使用Javascript编写XmlHttpRequest来下载文件时,是否可以通过托管Javascript页面的网站下载该文件?

For example, I have a web page hosted on server A. The javascript in the web page will download a file from server B. Will the file be downloaded through server A? 例如,我有一个网页托管在服务器A上。网页中的javascript将从服务器B下载文件。文件是否会通过服务器A下载? The reason for my confusion is due to CORS. 我感到困惑的原因是由于CORS。 I had to put a CORS header in the responses of server B, white-listing server A's domain. 我必须在服务器B(将服务器A的域列入白名单)的响应中放入CORS标头。 So this is confusing me a bit, since Javascript is supposed to client-side. 因此,这让我有些困惑,因为Javascript应该在客户端使用。

When I want to download a file by making the XmlHttpRequest in Javascript from a seperate server, will the file be downloaded through the website hosting the Javascript page? 当我想通过从单独的服务器使用Javascript编写XmlHttpRequest来下载文件时,是否可以通过托管Javascript页面的网站下载该文件?

No, of course not. 不,当然不。 The URL doesn't point to that site. 该URL指向该网站。

The reason for my confusion is due to CORS. 我感到困惑的原因是由于CORS。 I had to put a CORS header in the responses of server B, white-listing server A's domain. 我必须在服务器B(将服务器A的域列入白名单)的响应中放入CORS标头。 So this is confusing me a bit, since Javascript is supposed to client-side. 因此,这让我有些困惑,因为Javascript应该在客户端使用。

It is. 它是。

The browser knows where the page hosting the JavaScript came from. 浏览器知道托管JavaScript的页面来自何处。

It knows that you are asking for a file from a different site. 它知道您要从其他站点请求文件。

The browser won't let your JavaScript read the file unless the server the file comes from says that the JavaScript on the other site is trusted. 浏览器不会让您的JavaScript读取文件,除非该文件所来自的服务器说另一个站点上的JavaScript受信任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM