使用 NodeJs 提供临时文件

编程入门 行业动态 更新时间:2024-10-13 08:19:47
本文介绍了使用 NodeJs 提供临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在构建一个 NodeJs SOAP 客户端.最初,我想象服务器(即节点 SOAP 客户端)将允许通过 REST API 下载文档(REST API 已通过身份验证).在 Google 和 SO 上花了很多时间之后,看起来这是不可能的.

I am building a NodeJs SOAP client. Originally, I imagined the server (ie the node SOAP client) would allow downloading documents through a REST API (the REST API is authenticated). After a good deal of time on Google and SO, looks like that is not possible.

这意味着当请求下载文档时,我必须对文档进行 SOAP 调用,并通过 AJAX 向 REST 客户端返回一个 URL.

That means when a document download is requested, I'll have to make a SOAP call for the document and return a URL to the REST client via AJAX.

为了做到这一点,我需要:

In order to do this I'll need to:

  • 在 Node 中临时创建一个文件
  • 获取其 URL 并返回到 Web 客户端
  • 请求文件并发送响应后,删除文件(出于安全目的)
  • 这是我的问题:

  • 是否已经有一个框架可以做到这一点?临时模块 可能是一种选择,但实际上我想在每次请求后删除,而不是在一段时间后删除.
  • 如果没有,我是否可以仅使用 NodeJs 文件系统来执行此操作,并使用 Express static模块?基本上我们会将静态模块修改为如下所示:

  • Is there already a framework that does this? the temp module might be an option, but really I'd like to delete after every request, not after a time period.
  • If not, can I do this just using the NodeJs File System, and Express static module? Basically we would modify the static module to look like this: return function static(req, res, next) { if ('GET' != req.method && 'HEAD' != req.method) return next(); var path = parse(req).pathname; var pause = utils.pause(req); /* My Added Code Here */ res.on('end', function(){ // delete file based on req URL }) /* end additions */ function resume() { next(); pause.resume(); } function directory() { if (!redirect) return resume(); var pathname = url.parse(req.originalUrl).pathname; res.statusCode = 301; res.setHeader('Location', pathname + '/'); res.end('Redirecting to ' + utils.escape(pathname) + '/'); } function error(err) { if (404 == err.status) return resume(); next(err); } send(req, path) .maxage(options.maxAge || 0) .root(root) .hidden(options.hidden) .on('error', error) .on('directory', directory) .pipe(res); };

  • res.on('end',... 是否有效?或者,我是否应该创建一些中间件来为指向临时文件的 URL 执行此操作?

    Is res.on('end',... vaild? Alternatively,should I create some middleware that does this for URLs pointing to the temporary files?

    推荐答案

    发现两个 SO 问题可以回答我的问题.所以显然我们不需要使用 express.static 中间件.我们只需要文件系统来下载文件:

    Found two SO questions that answer my question. So apparently we don't need to use the express.static middleware. We just need the filesystem to download a file:

    app.get('/download', function(req, res){ var file = __dirname + '/upload-folder/dramaticpenguin.MOV'; res.download(file); // Set disposition and send it. });

    如果我们想流然后删除,请遵循:

    app.get('/download', function(req, res){ var stream = fs.createReadStream('<filepath>/example.pdf', {bufferSize: 64 * 1024}) stream.pipe(res); var had_error = false; stream.on('error', function(err){ had_error = true; }); stream.on('close', function(){ if (!had_error) fs.unlink('<filepath>/example.pdf'); });

    更多推荐

    使用 NodeJs 提供临时文件

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

    发布评论

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

    >www.elefans.com

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