Skip to content

Instantly share code, notes, and snippets.

@letswritetw
Created November 25, 2021 14:37
Show Gist options
  • Save letswritetw/8779a49d7eefd522a05f6948bd863f54 to your computer and use it in GitHub Desktop.
Save letswritetw/8779a49d7eefd522a05f6948bd863f54 to your computer and use it in GitHub Desktop.
nodejs-ftp-upload
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