Server side
Use + to implement the server side
const express = require("express"); const app = express(); const axios = require('axios'); ('port', || 8082); // Static resource directory, placed here in the root directory, this is not allowed in the production environment((__dirname)); // Start a server with port 8082(('port'), () => { ("http://localhost:" + ('port')); });
Prepare Base64, HMAC-SHA1, MD5 to implement signature authentication
See:/api/authorization/#_5
const crypto = require("crypto"); // MD5 function MD5(value) { return crypto .createHash("md5") .update(value) .digest("hex"); } // Base64 function base64(value) { return (value).toString("base64"); } // hmacsha1 function hmacsha1(secret, value) { return ('sha1', secret).update(value, 'utf-8').digest().toString('base64'); }
Upload and delete interfaces
const date = new Date().toGMTString(); const bucketname = ""; // Space nameconst key = ""; // Operatorconst secret = ""; // passwordconst upyunUrl = '/' // Upload ("/api/token/upload", (req, res) => { let fileName = (() * 100000000) >>> 0; let expiration = ((() / 1000) >>> 0) + 30 * 60; // The expiration time of the request, UNIX UTC timestamp, unit seconds. It is recommended to set to 30 minutes /api/form_api/ let method = "POST"; let policy = base64( ({ bucket: bucketname, // "save-key": "/" + fileName + "{.suffix}", "save-key": "/{filename}{.suffix}", expiration: expiration }) ); let authorization = "UPYUN " + key + ":" + hmacsha1(MD5(secret), method + "&/" + bucketname + "&" + policy); ({ msg: "OK", code: 200, data: { authorization: authorization, policy: policy } }); }); // Delete ('/api/token/del', (req, res) => { let 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 => { ({ msg: "OK", code: 200, data: {} }); }).catch(err => { ('err', err) }) })
Cross-domain interface call
const cors = require('cors'); // CORS @see /expressjs/cors (cors());
front end
Front-end usage implementation
Introduced
<link rel="stylesheet" type="text/css" href="/[email protected]/dist/css/"> <script src="/axios/dist/"></script> <!-- HTML --> <div > <div class="card" style="margin: 50px auto; width: 300px;"> <div class="card-body"> <h5 class="card-title">UPYun Upload & Delete</h5> <div class="card-text"> <div class="form-group"> <label for="file">Upload</label> <input type="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>
Introduce , Axios
<script src="/[email protected]/dist/"></script> <script src="/axios/dist/"></script>
JS
const upUrl = '/' // +Space name, such as: /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(); ("file", [0]) ('policy', ) ("authorization", ) axios({ method: 'POST', url: upUrl, data: formData }).then(res => { res = res || {} if ( !== 200) { ('error') return } let data = || {} () alert('Success') }).catch(err => { (err); }); }); }, onDel: function (item, index) { (index, 1) ({ url: baseApi + 'token/del', method: 'GET', params: { item: encodeURI(item) } }).then(res => { alert('Deleted.') }).catch(err => { (err) }) } }, mounted () { uploadInput = $('file') } }) // DOM gets the element function $ (el) { return (el) } // Get token function getToken (fn) { let token = ('token'); token = (token) || {}; let nowTime = (); if (nowTime < && && ) { fn(token) return } axios({ method: 'get', url: baseApi + 'token/upload' }) .then(res => { let data = || {} data = || {} const authorization = const policy = const expired = ((() / 1000) >>> 0) + 30 * 60; token = { authorization, policy, expired } fn(token) ('token', (token)) }); }
Project source code
/givebest/
Summarize
The above is what the editor introduced to Youpaiyun to realize file upload and delete. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!