您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用 Node.js怎么實現一個文件上傳、刪除功能,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
Node.js 服務端
使用 Node.js + Express.js 實現 服務端
const express = require("express"); const app = express(); const axios = require('axios'); app.set('port', process.env.PORT || 8082); // 靜態資源目錄,這里放在了根目錄,生產環境不允許這樣 app.use(express.static(__dirname)); // 啟動一個端口為 8082 的服務器 app.listen(app.get('port'), () => { console.log("http://localhost:" + app.get('port')); });
準備 Base64、HMAC-SHA1、MD5 實現簽名認證
詳見:http://docs.upyun.com/api/authorization/#_5
const crypto = require("crypto"); // MD5 function MD5(value) { return crypto .createHash("md5") .update(value) .digest("hex"); } // Base64 function base64(value) { return Buffer.from(value).toString("base64"); } // hmacsha1 function hmacsha1(secret, value) { return crypto.createHmac('sha1', secret).update(value, 'utf-8').digest().toString('base64'); }
上傳、刪除接口
const date = new Date().toGMTString(); const bucketname = ""; // 空間名 const key = ""; // 操作員 const secret = ""; // 密碼 const upyunUrl = 'http://v0.api.upyun.com/' // Upload app.get("/api/token/upload", (req, res) => { let fileName = (Math.random() * 100000000) >>> 0; let expiration = ((Date.now() / 1000) >>> 0) + 30 * 60; // 請求的過期時間,UNIX UTC 時間戳,單位秒。建議設為 30 分鐘 http://docs.upyun.com/api/form_api/ let method = "POST"; let policy = base64( JSON.stringify({ bucket: bucketname, // "save-key": "/" + fileName + "{.suffix}", "save-key": "/{filename}{.suffix}", expiration: expiration }) ); let authorization = "UPYUN " + key + ":" + hmacsha1(MD5(secret), method + "&/" + bucketname + "&" + policy); res.json({ msg: "OK", code: 200, data: { authorization: authorization, policy: policy } }); }); // Delete app.get('/api/token/del', (req, res) => { let item = req.query.item; let method = "DELETE" let authorization = "UPYUN " + key + ":" + hmacsha1(MD5(secret), method + '&/' + bucketname + item + '&'+ date); axios({ url: upyunUrl + bucketname + item, method: 'DELETE', headers: { 'Authorization': authorization, 'Date': date } }).then(response => { res.json({ msg: "OK", code: 200, data: {} }); }).catch(err => { console.log('err', err) }) })
跨域接口調用
const cors = require('cors'); // CORS @see https://github.com/expressjs/cors app.use(cors());
前端
前端使用 Vue.js 實現
引入 Bootstrap.css
<link rel="stylesheet" type="text/css" href="https://unpkg.com/bootstrap@4.1.3/dist/css/bootstrap.css"> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> <!-- HTML --> <div id="app"> <div class="card" > <div class="card-body"> <h6 class="card-title">UPYun Upload & Delete</h6> <div class="card-text"> <div class="form-group"> <label for="file">Upload</label> <input type="file" id="file" class="form-control-file" @change="onChange"> <div class="form-text text-muted"> <ul> <li v-for="(item, index) in files"> {{item}} <a href="javascript:;" rel="external nofollow" @click="onDel(item, index)">Del</a> </li> </ul> </div> </div> </div> </div> </div> </div>
引入 Vue.js、Axios
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
JS
const upUrl = 'http://v0.api.upyun.com/' // +空間名,如:http://v0.api.upyun.com/yun-temp const baseApi = 'http://localhost:8082/api/' let uploadInput; let app = new Vue({ el: '#app', data: { files: [] }, methods: { onChange: function () { getToken(token => { let formData = new FormData(); formData.append("file", uploadInput.files[0]) formData.append('policy', token.policy) formData.append("authorization", token.authorization) axios({ method: 'POST', url: upUrl, data: formData }).then(res => { res = res || {} if (res.status !== 200) { console.log('error') return } let data = res.data || {} this.files.push(data.url) alert('Success') }).catch(err => { console.log(err); }); }); }, onDel: function (item, index) { this.files.splice(index, 1) axios.request({ url: baseApi + 'token/del', method: 'GET', params: { item: encodeURI(item) } }).then(res => { alert('Deleted.') }).catch(err => { console.log(err) }) } }, mounted () { uploadInput = $('file') } }) // DOM 獲取元素 function $ (el) { return document.getElementById(el) } // 獲取 token function getToken (fn) { let token = window.localStorage.getItem('token'); token = JSON.parse(token) || {}; let nowTime = Date.now(); if (nowTime < token.expired && token.authorization && token.policy) { fn(token) return } axios({ method: 'get', url: baseApi + 'token/upload' }) .then(res => { let data = res.data || {} data = data.data || {} const authorization = data.authorization const policy = data.policy const expired = ((Date.now() / 1000) >>> 0) + 30 * 60; token = { authorization, policy, expired } fn(token) window.localStorage.setItem('token', JSON.stringify(token)) }); }
上述內容就是使用 Node.js怎么實現一個文件上傳、刪除功能,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。