<label for='my_file' class="theme-color"> <mu-icon left value="backup"></mu-icon> 修改頭像 </label> <input type="file" ref="upload" name="avatar" id='my_file' style="display:none;" accept="image/jpg" @change="changeAvatar" />
axios接口node
let ChangeAvatar = (img) => axios({ url: '/user/changeavatar', method: 'post', anync: true, contentType: false, processData: false, data: img })
js部分調用封裝的接口ios
methods: { changeAvatar (event) { let img = event.target.files[0]; let size = img.size; if (size > 3145728) { alert('請選擇3M之內的圖片!'); return false; } let Form = new FormData(); Form.append('avatar', img, this.avatar_name); API.ChangeAvatar(Form) .then((response) => { console.log(response) }) .catch((error) => { console.log(error) }) } }
const fileUpload = require('express-fileupload'); app.use(fileUpload()); app.post('/user/changeavatar', function(req, res) { console.log(req.files); // the uploaded file object let avatar = req.files.avatar; // Use the mv() method to place the file somewhere on your server avatar.mv('dist/static/img/avatar/'+req.files.avatar.name+'.jpg', function(err) { if (err) return res.status(500).send(err); res.send('File uploaded!'); }); })
代碼運行,成功將圖片上傳到了指定目錄ajax