我的确认为在请求中映射Content-Type头也是可行的。即使在您上传没有扩展名的文件的情况下,这也可以工作。 (当文件名没有在请求延期)
假设您发送使用HTTP POST数据:
POST /upload2 HTTP/1.1
Host: localhost:7098
Connection: keep-alive
Content-Length: 1047799
Accept: */*
Origin: http://localhost:63342
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36
Content-Type: multipart/form-data; boundary=---- WebKitFormBoundaryPDULZN8DYK3VppPp
Referer: http://localhost:63342/Admin/index.html? _ijt=3a6a054pasorvrljf8t8ea0j4h
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,az;q=0.6,tr;q=0.4
Request Payload
------WebKitFormBoundaryPDULZN8DYK3VppPp
Content-Disposition: form-data; name="image"; filename="blob"
Content-Type: image/png
------WebKitFormBoundaryPDULZN8DYK3VppPp--
这名Content-Type头包含数据的MIME类型。 将这个MIME类型映射到扩展将为您带来文件扩展名:)。
的RESTify BodyParser在一个属性这个标题变换名称为型
File {
domain:
Domain {
domain: null,
_events: { .... },
_eventsCount: 1,
_maxListeners: undefined,
members: [ ... ] },
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
size: 1047621,
path: '/tmp/upload_2a4ac9ef22f7156180d369162ef08cb8',
name: 'blob',
**type: 'image/png'**,
hash: null,
lastModifiedDate: Wed Jul 20 2016 16:12:21 GMT+0300 (EEST),
_writeStream:
WriteStream {
... },
writable: true,
domain:
Domain {
...
},
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
path: '/tmp/upload_2a4ac9ef22f7156180d369162ef08cb8',
fd: null,
flags: 'w',
mode: 438,
start: undefined,
pos: undefined,
bytesWritten: 1047621,
closed: true }
}
你可以用这个头,做了扩展名映射(子等)手动,但也有现成的图书馆为此。下面两个是结果的最前面,当我做了谷歌搜索
及其用法也很简单:
app.post('/upload2', function (req, res) {
console.log(mime.extension(req.files.image.type));
}
上面的代码将打印到控制台上png。
http://stackoverflow.com/questions/10431845/node-js-file-system-get-file-type repeat? –
是不是关于MIME类型的问题,我想要文件扩展名 – georgesamper
@Jojje所以下定决心。标题*和*您的问题的正文要求一种辨别文件*类型*的方式。那么你的问题究竟是什么?告诉我们一些代码,告诉我们你期望它做什么,以及它在做什么。 –