简体   繁体   English

如何在节点js中下载文件

[英]How to download a file in node js

I am trying to download array of objects in .csv format.我正在尝试以 .csv 格式下载对象数组。 below is the code snippet which converts an array to .csv and get stored in the file file.csv.下面是将数组转换为 .csv 并存储在文件 file.csv 中的代码片段。

let downloadHelper = function(records){
    let csvwriter = require('csv-writer'); 
    createCsvWriter = csvwriter.createObjectCsvWriter;

    const csvWriter = createCsvWriter({
        path: './file.csv'

    csvWriter.writeRecords(records).then(() => {
        console.log('Done');
    });
} 

I need to download the file.csv to my local.我需要将 file.csv 下载到我的本地。 tried using requests, didn't help as it is accepting only http requests.尝试使用请求,没有帮助,因为它只接受 http 请求。 no clue, how to proceed..... Please help不知道,如何继续..... 请帮忙

You did not provide us a lot of information.你没有向我们提供很多信息。 But with Express you could do:但是使用 Express 你可以做到:

app.get("/", (req, res) => {
  res.download("./file.csv", "your-custom-name.csv");
});

If this does not help you, please provide more info about the context, framework you are using and what front.如果这对您没有帮助,请提供有关上下文、您正在使用的框架以及前端的更多信息。

Thank you谢谢

For example, you can use Express like this:例如,您可以像这样使用 Express:

// Libs
const express = require('express');
const http = require('http');
const path = require('path');

// Setup
const port = 8080;
const app = express();
const httpServer = http.createServer(app);

// http://localhost:8080/download
app.get('/download', (req, res) => {
  res.sendFile(path.resolve(__dirname, './file.csv'));
});
// http://localhost:8080/csv/file.csv
app.use('/csv', express.static(path.resolve(__dirname, './csv_files/')));

// Run HTTP server
httpServer.listen(port, () => console.log('Server is listening on *:' + port));

If you run this snippet, you can open http://localhost:8080/download and ./file.csv would be downloaded.如果您运行此代码段,您可以打开http://localhost:8080/downloadhttp://localhost:8080/download ./file.csv。

Following part of code is responsible for that:以下部分代码负责:

app.get('/download', (req, res) => {
  res.sendFile(path.resolve(__dirname, './file.csv'));
});

Or if you want to give access to the whole directory ./csv_files/ you can do this:或者,如果您想访问整个目录./csv_files/您可以这样做:

app.use('/csv', express.static(path.resolve(__dirname, './csv_files/')));

Just create ./csv_files/foo.csv file and go to http://localhost:8080/csv/foo.csv .只需创建./csv_files/foo.csv文件并转到http://localhost:8080/csv/foo.csv

Does it make sense to you?这对你有意义吗?

PS Working example: PS工作示例:

// Libs
const express = require('express');
const http = require('http');
const path = require('path');
const fs = require('fs');

// Setup
const port = 8080;
const app = express();
const httpServer = http.createServer(app);

// http://localhost:8080/download
app.get('/download', (req, res) => {
  const filename = path.resolve(__dirname, './file' + (new Date()).getTime() + '.csv');
  fs.writeFileSync(filename, 'foo,bar,baz');
  res.sendFile(filename);
});

httpServer.listen(port, () => console.log('Server is listening on *:' + port));

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

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