在node.js中获取上传的文件名/路径 [英] get uploaded file name/path in node.js

查看:497
本文介绍了在node.js中获取上传的文件名/路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取上传的文件名/路径的名称以在node.js中操作它?我想将文件从临时文件夹移动到客户文件夹。

解决方案

Node.JS不会自动将上传的文件保存到磁盘。您将不得不通过 multipart / form-data 内容#http_class_http_serverrequestrel =nofollow noreferrer> request data end 事件。也可以使用一个库来完成所有的工作,比如 connect / express bodyParser multipart 中间件(完整示例):

  var fs = require('fs'); 
var express = require('express');
var app = express();

//`bodyParser`包含`multipart`
app.use(express.bodyParser());

app.post('/',function(req,res,next){
//假设< input type =filename =upload>

var path = req.files.upload.path;
var name = req.files.upload.name;

// copy ...
} );

或者使用 formidable connect 用于 multipart 中间件( complete例如)。



而对于 // copy ... 评论,请参阅如何复制文件?


How do I get the name of an uploaded file name/path to manipulate it in node.js? I want move the file from the temp folder to a customer folder.

解决方案

Node.JS doesn't automatically save uploaded files to disk. You'll instead have to read and parse the multipart/form-data content yourself via the request's data and end events.

Or, you can use a library to do that all for you, such as connect/express for its bodyParser or multipart middlewares (complete example):

var fs = require('fs');
var express = require('express');
var app = express();

// `bodyParser` includes `multipart`
app.use(express.bodyParser());

app.post('/', function(req, res, next){
  // assuming <input type="file" name="upload">

  var path = req.files.upload.path;
  var name = req.files.upload.name;

  // copy...
});

Or use formidable directly, which connect uses for the multipart middleware (complete example).

And, for the // copy... comment, see How to copy a file?.

这篇关于在node.js中获取上传的文件名/路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆