冗长的下载数据为CSV文件冗长、文件、数据、CSV

2023-09-13 04:09:58 作者:捷豹路虎少年梦i

我需要下载在视图中为CSV文件显示表的内容。我有code正确格式化表的内容,以便它可以被存储为.csv文件(例如,列分隔符是逗号,表中的每个记录是一个新行)。

I need to download the contents of a table shown in the view as a CSV file. I have code to format the contents of the table properly so that it can be stored as a .csv file (for example, the column separator is a comma, each record of the table is in a new line).

所以,在我的控制器,我有以下的code:

So, in my controller, I have the following code:

window.location.href = '/download?data=' + encodeURIComponent($scope.csvString);

我使用了的NodeJS服务器部分。在这里,我有以下途径处理:

I am using NodeJs for the server part. Here, I have the following route handler:

app.get('/download', function (req, res) {
    var data = req.query.data;

    res.attachment('data.csv');
    res.setHeader('Content-Type', 'text/csv');
    res.end(data);
});

这工作完全正常。下载作品和数据下载的文件是在完美的CSV格式。没有问题。

This works perfectly fine. The download works and the data in the file downloaded is in perfect CSV format. No issues.

当有表中的记录太多的问题出现了,那就是当数据是巨大的。由于这个原因,查询参数确实是巨大的,我认为它超过了极限,因此,GET请求以400(错误请求)返回错误 - 请注意,根据我的分析,请求不会到达服务器,而不是我观察该请求使用Chrome开发者工具失败。在浏览器中,URL根本 www.my_domain.com/download?data=the_lengthy_data 。而已。该网页是空白/空的,没有文件被下载。

The issue arises when there are too many records in the table, that is when the data is huge. Due to this, the query parameter is really huge and I assume that it exceeds the limit and thus, the GET request returns with a 400 (Bad Request) error - note that according to my analysis, the request never reaches the server, instead I observed that the request failed using the Chrome Developer Tools. In the browser, the URL simply is www.my_domain.com/download?data=the_lengthy_data. That's it. The web page is blank / empty and no file is downloaded.

因此​​,我想知道我怎么可以下载冗长的数据?我碰到所有的例子做一个GET请求,因此我必须要通过冗长的数据作为查询参数。有一些其他的方法呢?

Thus, I wish to know how I can download the lengthy data? All examples that I came across do a GET request and thus I have to pass the lengthy data as a query parameter. Is there some other approach?

推荐答案

不是一个真正的回答你的问题,但是从外观上来看,你试图力在客户端上产生的数据的下载。那么在这种情况下,你可以创建一个数据的URI,直接在它的数据。

Not really an answer to your question, but from the looks of it, you are trying to "force" a download of data generated on the client. Then in this case, you could create a data URI, that has the data directly in it.

您的角度JS模板:

<a href="data:base64,{{btoa(csvString)}}" download>Download CSV</a>

您控制器:

$scope.btoa = function (val) {
    return btoa(val);
};

这样一来,你的DOM应该是这样的:

As a result, your DOM should look like:

<!-- The link contains data to the CSV string "a,b,c" -->
<a href="data:text/csv;base64,YSxiLGM=" download>Download CSV</a>

和点击链接下载CSV文件。

And clicking the link downloads the CSV file.

修改

您也可以强制使用一种称为库字符串的下载 FileSaver.js 。

You could also force download of strings using a library called FileSaver.js.

所以像往常一样,你会得到CSV,但这个时候,你会将它转换为BLOB。

So as usual, you would get the CSV, but this time, you would convert it to a BLOB.

var csvBlob = new Blob([$scope.csvString], { type: 'text/csv;charset=utf-8' });

然后,你可以调用FileSaver.js的saveAs 功能。

saveAs(csvBlob, 'mydata.csv');
 
精彩推荐
图片推荐