Node.js 节点文件名
我一直在使用node命令对文件名进行缓冲。我试过form.on,但它已经不受欢迎了� 替换字符。因此文件名“ěšř.png”变为����.png'而不是escr.pngNode.js 节点文件名,node.js,formidable,Node.js,Formidable,我一直在使用node命令对文件名进行缓冲。我试过form.on,但它已经不受欢迎了� 替换字符。因此文件名“ěšř.png”变为����.png'而不是escr.png form.on('end', function(fields, files) { var new_location = 's:/localhost/nodejs/'; for(var i = 0; i < this.openedFiles.length; i++){ var te
form.on('end', function(fields, files) {
var new_location = 's:/localhost/nodejs/';
for(var i = 0; i < this.openedFiles.length; i++){
var temp_path = this.openedFiles[i].path;
var file_name = slug(this.openedFiles[i].name);
fs.copy(temp_path, new_location + file_name, function(err) {
if (err) {
console.error(err);}
else {
console.log("success!")
}
});
}
});
form.on('end',函数(字段、文件){
var new_location='s:/localhost/nodejs/';
for(var i=0;i
您没有为“slug”函数提供代码,所以我提供了新的slagify函数。
您的代码中也没有给出“OpenedFile”,我假设所有其他内容都可以
在你的核心之上需要
const path=require('path');
使用此代码
form.on('end', function(fields, files) {
var new_location = 's:/localhost/nodejs/';
for(var i = 0; i < this.openedFiles.length; i++){
var temp_path = this.openedFiles[i].path;
var baseName = path.basename(this.openedFiles[i].name);
var extName = path.extname(this.openedFiles[i].name);
var file_name = slugify(baseName) + '.' + extName;
fs.copy(temp_path, new_location + file_name, function(err) {
if (err) {
console.error(err);}
else {
console.log("success!")
}
});
}
});
function slugify(text){
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
form.on('end',函数(字段、文件){
var new_location='s:/localhost/nodejs/';
for(var i=0;i