1、vue
<template>
<div>
<van-uploader v-model="fileList" :after-read="afterRead" multiple :max-count="1" />
<van-button type="primary" @click="cctt">测试</van-button>
</div>
</template>
<script>
import Vue from 'vue';
import { Uploader } from 'vant';
import { Button } from 'vant';
Vue.use(Button);
Vue.use(Uploader);
export default {
name: "index",
data () {
return {
fileList: [],
};
},
methods:{
cctt(){
// console.log("上传")
// console.log(this.fileList)
var postData={"pic":"小明明"}
this.TpExecute("/testFunc","post","","上传中,请稍后......",function(res){
//
alert(res.data)
})
},
afterRead(file) {
// 此时可以自行将文件上传至服务器
console.log("上传后直接服务器")
console.log(file.content);
let postData={pic:file.content}
this.TpExecute("/testUpload","post",postData,"上传中,请稍后......",function(res){
//
alert("上传成功!")
})
},
}
}
</script>
<style lang="scss">
@import "../../assets/css/public.scss";
</style>
2、python
import json
import urllib
from datetime import datetime
from flask import Response,Flask, render_template, request, send_file, send_from_directory,make_response
from flask_cors import CORS
app = Flask(__name__)
def getInputtFilename():
return './pictureFile/'+ datetime.now().strftime('%Y-%m-%d-%H-%M-%S%f')+".jpg"
#
CORS(app, resources=r'/*')
@app.route('/testFunc', methods = ['POST','GET'])
def testFunc():
return "123456!"
CORS(app, resources=r'/*')
@app.route('/testUpload', methods = ['POST','GET'])
def testUpload():
inpath = getInputtFilename()
data = json.loads(request.get_data())
pictureFile = data["pic"]
urllib.request.urlretrieve(pictureFile, filename=inpath)
return "Ok"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=xxxx, threaded=False)