Created
November 25, 2021 14:37
-
-
Save letswritetw/8779a49d7eefd522a05f6948bd863f54 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
import 'idempotent-babel-polyfill'; | |
// POST 給後端的 URL | |
const uploadUri = 'https://www.example.tw/uploadFileToFTP'; | |
// 處理每一個 POST | |
function fileUploadHandler(file) { | |
return new Promise((resolve, reject) => { | |
let formData = new FormData(); | |
formData.append('clientFile', file); | |
fetch(uploadUri, { | |
method: 'POST', | |
body: formData | |
}).then(res => res.text()) | |
.then(res => resolve(res)) | |
.catch(err => reject(err)); | |
}) | |
} | |
// 點擊「送出」按鈕時要執行 | |
const inputFiles = document.querySelectorAll('[type="file"]'); | |
const uploadBtn = document.getElementById('upload'); | |
uploadBtn.addEventListener('click', async () => { | |
for(let file of inputFiles) { | |
if(file.files[0]) { | |
let uploadResult = await fileUploadHandler(file.files[0]); | |
console.log(file.files[0].name, uploadResult); | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment