iT邦幫忙

2023 iThome 鐵人賽

DAY 14
0
SideProject30

Electron Angular軟體架構與簡易功能實作學習路程實記系列 第 14

Day 14-功能規格設計-Api開發設計-4

  • 分享至 

  • xImage
  •  

express-fileupload安裝

$ npm install express-fileupload
$ npm install @types/express-fileupload

單檔下載儲存

PictureController.use(fileUpload());
PictureController.post('/PictureUpload', function(req, res) {
  let uploadPath;

  if (!req.files || Object.keys(req.files).length === 0) {
    return res.status(400).send('No files were uploaded.');
  }
  let uploadedFile: fileUpload.UploadedFile;
  if (req.files.sampleFile instanceof Array) {
    uploadedFile = req.files.sampleFile[0];
  }
  else {
    uploadedFile = req.files.sampleFile;
    uploadPath = __dirname + '/' + uploadedFile.name;
    uploadedFile.mv(uploadPath, function(err) {
      res.send('File uploaded!');
    });
  }
});

export default PictureController;

前端測試頁面

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
  <form ref='uploadForm'
        id='uploadForm'
        action='http://localhost:3000/PictureUpload'
        method='post'
        encType="multipart/form-data">
    <input type="file" name="sampleFile" />
    <input type='submit' value='Upload!' />
  </form>`
</body>
</html>

功能測試

https://ithelp.ithome.com.tw/upload/images/20230929/20124238xkcY7YvI22.png


上一篇
Day 13- 功能規格設計-後端Api開發設計-3
下一篇
Day15-功能規格設計-Api開發設計-5
系列文
Electron Angular軟體架構與簡易功能實作學習路程實記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言