Created
November 25, 2021 14:39
-
-
Save letswritetw/228e6c7cd2ef4a1512c248799e724340 to your computer and use it in GitHub Desktop.
nodejs-ftp-upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const cors = require('cors'); | |
const app = express(); | |
const fileupload = require('express-fileupload'); | |
app.use(fileupload(), cors()) | |
const jsftp = require('jsftp'); | |
const Ftp = new jsftp({ | |
host: '這邊填FTP的主機', | |
port: 21, // FTP 的連接埠,預設是 21 | |
user: '這邊填使用者名稱', | |
pass: '這邊填密碼' | |
}); | |
app.post('/uploadFileToFTP', (req, res) => { | |
const file = req.files.clientFile; // 傳來的檔案 | |
const fileName = file.name; // 檔名 | |
const fileData = file.data; // 要傳到 FTP 上的檔案 | |
Ftp.put(fileData, '這邊填寫要傳到FTP的哪個路徑上' + fileName, err => { | |
if(!err) { | |
res.send('Upload Done'); | |
} else { | |
res.send(err); | |
} | |
}); | |
}); | |
const PORT = process.env.PORT || 8080; | |
app.listen(PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment