요청에 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--
여기서 name-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 를 콘솔에 인쇄 합니다.