app.js 主文件中的 toString 未定义 - NodeJS
toString undefined in app.js main file - NodeJS
我正在尝试获取上传文件的文件名,但在 nodejs 的主应用程序文件中进行如下所示的字符串操作时出现错误:
var fileName = file.split('path:')[1]
.split('\',')[0]
.split(dir)[1]
.toString()
.replace(/\/g, '')
.replace(/\//g, '');
我在 .toString()
遇到错误,不知道为什么。
错误堆栈跟踪
TypeError: Cannot read property 'toString' of undefined
at C:\Users\murli\NodeProjects\SpeechExpress\app.js:57:76
at IncomingForm.<anonymous> (C:\Users\murli\NodeProjects\SpeechExpress\node_modules\formidable\lib\incoming_form.js:105:9)
at emitNone (events.js:86:13)
at IncomingForm.emit (events.js:185:7)
at IncomingForm._maybeEnd (C:\Users\murli\NodeProjects\SpeechExpress\node_modules\formidable\lib\incoming_form.js:553:8)
at C:\Users\murli\NodeProjects\SpeechExpress\node_modules\formidable\lib\incoming_form.js:230:12
at WriteStream.<anonymous> (C:\Users\murli\NodeProjects\SpeechExpress\node_modules\formidable\lib\file.js:74:5)
at WriteStream.g (events.js:291:16)
at emitNone (events.js:91:20)
at WriteStream.emit (events.js:185:7)
代码
app.use('/uploadFile', function(request, response){
// parse a file upload
var mime = require('mime');
var formidable = require('formidable');
var util = require('util');
var form = new formidable.IncomingForm();
var dir = !!process.platform.match(/^win/) ? '\public\uploads\' : '/public/uploads/';
form.uploadDir = __dirname + dir;
console.log("Test 99: " + form.uploadDir);
form.keepExtensions = true;
form.maxFieldsSize = 10 * 1024 * 1024;
form.maxFields = 1000;
form.multiples = false;
console.log("Test 100: ");
form.parse(request, function(err, fields, files) {
var file = util.inspect(files);
console.log("form parse file: "+file + "\n" + files);
response.writeHead(200, getHeaders('Content-Type', 'application/json'));
var fileName = file.split('path:')[1].split('\',')[0].split(dir)[1].toString().replace(/\/g, '').replace(/\//g, '');
var fileURL = 'http://' + app.address + ':' + port + '/public/uploads/' + fileName;
console.log('fileURL: ', fileURL);
response.write(JSON.stringify({
fileURL: fileURL
}));
response.end();
});
});
文件内容
{ file:
File {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 114898,
path: 'C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm',
name: '1234.webm',
type: 'video/webm',
hash: null,
lastModifiedDate: 2017-09-19T22:18:03.330Z,
_writeStream:
WriteStream {
_writableState: [Object],
writable: false,
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
path: 'C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm',
fd: null,
flags: 'w',
mode: 438,
start: undefined,
autoClose: true,
pos: undefined,
bytesWritten: 114898,
closed: true } } }
我需要的就是这个
upload_3c927e85105d0fd5d873e84952821531.webm
由此
C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm
喜欢这样
var f = File.path.split('\');
var fileName = f[f.length-1];
用path.basename
有什么不省心的呢?
var path = require('path');
path.basename('/foo/bar/baz/asdf/quux.html');
// Returns: 'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', '.html');
// Returns: 'quux'
All I need is this
upload_3c927e85105d0fd5d873e84952821531.webm
from this
C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm
然后简单地:
let path = 'C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm',
filename = path.split('\').pop();
并用正确的引用替换 path
我建议你使用正则表达式来处理它。
E:G
let file = 'C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm'
let fileName = file.match(/upload_\w+.webm$/)[0];
let fileURL = 'http://' + app.address + ':' + port + '/public/uploads/' + fileName;
// ....The rest of the code.
你好像用的是node,所以我建议你使用官方的路径对象:
const path = require("path")
const myFileName = "C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm"
const unixFile = "/home/test/jhgfd.h"
const obj = path.parse(myFileName)
console.log(obj.name)
console.log(obj)
最好的是它也适用于 Linux。您想要的属性名为名称。
这个问题有 2 个问题。
首先是如何从完整的目录字符串中提取文件名。因为你使用 node.js,它带有一个方便的函数 path.basename(filename)
.
第二个问题,首先是如何获取路径。
Formidable 提供文件,这是一个包含所有文件的简单对象字面量。您的文件输入名称似乎被称为 file,.. 所以要访问路径,您只需执行 files.file.path
.
所以在结合这两个之后我们有.. path.basename(files.file.path)
,你应该得到你的文件名,在这种情况下是 -> upload_3c927e85105d0fd5d873e84952821531.webm
我正在尝试获取上传文件的文件名,但在 nodejs 的主应用程序文件中进行如下所示的字符串操作时出现错误:
var fileName = file.split('path:')[1]
.split('\',')[0]
.split(dir)[1]
.toString()
.replace(/\/g, '')
.replace(/\//g, '');
我在 .toString()
遇到错误,不知道为什么。
错误堆栈跟踪
TypeError: Cannot read property 'toString' of undefined
at C:\Users\murli\NodeProjects\SpeechExpress\app.js:57:76
at IncomingForm.<anonymous> (C:\Users\murli\NodeProjects\SpeechExpress\node_modules\formidable\lib\incoming_form.js:105:9)
at emitNone (events.js:86:13)
at IncomingForm.emit (events.js:185:7)
at IncomingForm._maybeEnd (C:\Users\murli\NodeProjects\SpeechExpress\node_modules\formidable\lib\incoming_form.js:553:8)
at C:\Users\murli\NodeProjects\SpeechExpress\node_modules\formidable\lib\incoming_form.js:230:12
at WriteStream.<anonymous> (C:\Users\murli\NodeProjects\SpeechExpress\node_modules\formidable\lib\file.js:74:5)
at WriteStream.g (events.js:291:16)
at emitNone (events.js:91:20)
at WriteStream.emit (events.js:185:7)
代码
app.use('/uploadFile', function(request, response){
// parse a file upload
var mime = require('mime');
var formidable = require('formidable');
var util = require('util');
var form = new formidable.IncomingForm();
var dir = !!process.platform.match(/^win/) ? '\public\uploads\' : '/public/uploads/';
form.uploadDir = __dirname + dir;
console.log("Test 99: " + form.uploadDir);
form.keepExtensions = true;
form.maxFieldsSize = 10 * 1024 * 1024;
form.maxFields = 1000;
form.multiples = false;
console.log("Test 100: ");
form.parse(request, function(err, fields, files) {
var file = util.inspect(files);
console.log("form parse file: "+file + "\n" + files);
response.writeHead(200, getHeaders('Content-Type', 'application/json'));
var fileName = file.split('path:')[1].split('\',')[0].split(dir)[1].toString().replace(/\/g, '').replace(/\//g, '');
var fileURL = 'http://' + app.address + ':' + port + '/public/uploads/' + fileName;
console.log('fileURL: ', fileURL);
response.write(JSON.stringify({
fileURL: fileURL
}));
response.end();
});
});
文件内容
{ file:
File {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 114898,
path: 'C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm',
name: '1234.webm',
type: 'video/webm',
hash: null,
lastModifiedDate: 2017-09-19T22:18:03.330Z,
_writeStream:
WriteStream {
_writableState: [Object],
writable: false,
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
path: 'C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm',
fd: null,
flags: 'w',
mode: 438,
start: undefined,
autoClose: true,
pos: undefined,
bytesWritten: 114898,
closed: true } } }
我需要的就是这个
upload_3c927e85105d0fd5d873e84952821531.webm
由此
C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm
喜欢这样
var f = File.path.split('\');
var fileName = f[f.length-1];
用path.basename
有什么不省心的呢?
var path = require('path');
path.basename('/foo/bar/baz/asdf/quux.html');
// Returns: 'quux.html'
path.basename('/foo/bar/baz/asdf/quux.html', '.html');
// Returns: 'quux'
All I need is this
upload_3c927e85105d0fd5d873e84952821531.webm
from this
C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm
然后简单地:
let path = 'C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm',
filename = path.split('\').pop();
并用正确的引用替换 path
我建议你使用正则表达式来处理它。
E:G
let file = 'C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm'
let fileName = file.match(/upload_\w+.webm$/)[0];
let fileURL = 'http://' + app.address + ':' + port + '/public/uploads/' + fileName;
// ....The rest of the code.
你好像用的是node,所以我建议你使用官方的路径对象:
const path = require("path")
const myFileName = "C:\Users\username\NodeProjects\SpeechExpress\public\uploads\upload_3c927e85105d0fd5d873e84952821531.webm"
const unixFile = "/home/test/jhgfd.h"
const obj = path.parse(myFileName)
console.log(obj.name)
console.log(obj)
最好的是它也适用于 Linux。您想要的属性名为名称。
这个问题有 2 个问题。
首先是如何从完整的目录字符串中提取文件名。因为你使用 node.js,它带有一个方便的函数 path.basename(filename)
.
第二个问题,首先是如何获取路径。
Formidable 提供文件,这是一个包含所有文件的简单对象字面量。您的文件输入名称似乎被称为 file,.. 所以要访问路径,您只需执行 files.file.path
.
所以在结合这两个之后我们有.. path.basename(files.file.path)
,你应该得到你的文件名,在这种情况下是 -> upload_3c927e85105d0fd5d873e84952821531.webm