繁体   English   中英

将文件捕获到 javascript 变量中而不是下载它

[英]capture file into javascript variable instead of downloading it

我正在编写一个访问网站的应用程序,该网站可以为我提供文件链接,如下所示: http : //www.thrustcurve.org/download.jsp?id=2199

如果我访问此链接,则会下载一个小文本文件。 我想做的是将此文本捕获到一个 javascript 变量中,以便我可以在其中搜索并提取我需要的数据。

这甚至可能吗?

更多细节:虽然我年纪大了,有很多编程经验,但我在 javascript/web/server/现代空间(想想 FORTRAN 77)中是个菜鸟。 我现在教高中物理,并且正在尝试构建一个基于网络的火箭模拟器,供我的学生在他们的 chromebook 上使用。 推力曲线网站的创建者慷慨地在网络上提供了有关火箭发动机的数据,但我需要一些只能在这些小文本文件中找到的数据。 也许可以在 chrome 书籍上使用下载的文件,但我真的不知道如何开始。 如果你有足够的耐心读到这里,你可以在 noragulfa.com 上看到我已经完成的 JavaScript 类型

您可以使用XMLHttpRequest来执行 HTTP 请求,但由于安全限制,浏览器会阻止对“外部域”的请求(因此,您只能从您的域下载文件)。 有关更多信息,请阅读跨域资源共享 (CORS)

要解决您的任务,您有多种选择:

1)从推力曲线网站下载所需文件并将它们存储在您的服务器上。 这是最好的选择,因为您将不依赖于外部服务器(此外,盗链可能会扰乱推力曲线网站的所有者)。 在这种情况下, XMLHttpRequest 将能够使用相对 URL 访问文件:

var url = '/thrustcurve-downloads/Estes_A8.eng';

2)联系推力曲线网站所有者并要求他从任何地方启用Access-Control-Allow-Origin 在这种情况下, XMLHttpRequest 将能够使用完整 URL 访问文件:

var url = 'http://www.thrustcurve.org/download.jsp?id=2199';

3)创建一个代理,将 HTTP 请求传递到推力曲线.org。 例如,由于您使用的是 nginx,您可以简单地将以下内容添加到您的配置文件中:

location /thrustcurve {
    proxy_pass http://www.thrustcurve.org/;
}

在这种情况下, XMLHttpRequest 将能够使用相对 URL 访问文件:

var url = '/thrustcurve/download.jsp?id=2199';

4)使用第三方代理(不是一个非常可靠的解决方案,但非常适合测试)。 例如,我将使用此选项。

 var url = 'http://cors-anywhere.herokuapp.com/http://www.thrustcurve.org/download.jsp?id=2199'; var xhr = new XMLHttpRequest(); xhr.onload = function () { console.log(xhr.response); }; xhr.open('GET', url); xhr.responseType = 'text'; xhr.send();

UPD:如何使用 XMLHttpRequest 和 PHP 下载文件的完整示例。

1) 在根服务器上创建文件thrustcurve.php ,内容如下:

<?php

// Change this to FALSE if don't want to store files locally
$store_files_locally = true;

$id = (int) filter_input(INPUT_GET, 'id');
if ($id > 0) {
    if ($store_files_locally) {
        // Specify the directory where you want to store engine files
        // It will create the directory if it doesn't exist
        $dir = __DIR__ . '/thrustcurve-downloads';
        if (!is_dir($dir) && !mkdir($dir, true, 0777)) {
            http_response_code(500);
            die('Cannot create the downloads directory');
        }

        // If file exists, load the engine from the local file
        $file = "{$dir}/{$id}.eng";
        if (is_file($file)) {
            $engine = file_get_contents($file);
            die($engine);
        }
    }

    // Download the engine file from the remote server
    $url = "http://www.thrustcurve.org/download.jsp?id={$id}";
    $engine = trim(@file_get_contents($url));

    // The downloaded file is considered valid engine only if it starts with semicolon
    if (strpos($engine, ';') === 0) {
        if ($store_files_locally) {
            file_put_contents($file, $engine);
        }

        die($engine);
    }
}

http_response_code(404);
echo "File #{$id} not found";

2) 要使用 JavaScript 下载文件,请使用以下命令:

var xhr = new XMLHttpRequest();
xhr.onload = function () {
    if (xhr.status === 200) {
        console.log(xhr.response);
    } else {
        console.error(xhr.response);
    }
};

xhr.open('GET', '/thrustcurve.php?id=2198');
xhr.responseType = 'text';
xhr.send();

暂无
暂无

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

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