Today, I spent a day building an ftp server by myself, and successfully obtained the pictures on my ftp server in the background and returned it to the front-end interface in real time.
var
ftp
=
require
(
'ftp'
),
ftp
=
new
ftp
(),
fs
=
require
(
'fs'
),
watch
=
require
(
'watch'
) // used to monitor directory changes
// Triggered when the ftp connection is successful
ftp
.
on
(
'ready'
,
function
(){
getlist
(
'.'
) //You can fill in the file path on the ftp server according to your own situation. What I want to get is all the files in the root directory of ftp
watch.
createMonitor(
'/xx/xxx',
monitor
=> {
monitor.
on(
"created",
function (
f,
stat) {
console.
log(
'created')
console.log(f)
})
})
});
ftp.
connect({
host:
'***.***.**.**', //自己ftp服务器地址
user:
'*****',
password:
'******'
});
//查找文件
function
getlist(
path){
//罗列出该文件夹内的文件列表
ftp.
list(
path,
function(
err,
list){
if(
err)
throw
err;
list.
forEach(
function(
item){
if(
item.
type===
'd'){
//文件夹其实是一种特殊的文件,因此这里还存在两个「文件夹」:'.'和'..',但我们对它们不做处理
if(
item.
name===
'.')
return;
if(
item.
name===
'..')
return;
//然后,历遍一下这个文件夹
getlist(
path+
'/'+
item.
name);
}
else{
if(
item.
size/
1024 >
100 ){
return
}
else{
let
picPath =
path+
'/'+
item.
name
picPath =
picPath.
substring(
1)
picPath =
'ftp://***.***.**.**'+
picPath
console.
log(
picPath)
return
picPath
}
}
});
});
}