使用vue-image-crop-upload2及错误记录
2019-05-07 本文已影响0人
一叶知秋_038b
使用方法:
npm install vue-image-crop-upload 加载


<div id="app">
<a class="btn" @click="toggleShow">设置头像</a>
<my-upload field="img"
:width="300"
:height="300"
url="/upload"
/*url 若是vue项目 前后端分离 要配好代理
如'/file':{
target:'http://localhost:9001/file',
changeOrigin:true,//允许跨域
pathRewrite: {
'^/file':'/'
},
},*/
:params="params" //格式为{k:v}
:headers="headers"
:value.sync="show"
img-format="png"></my-upload> //最终得到的图片格式
<img :src="imgDataUrl">
</div>
<script>
import 'babel-polyfill'; // es6 shim
import Vue from 'vue';
import myUpload from 'vue-image-crop-upload/upload-1.vue';
new Vue({
el: '#app',
data: {
show: true,
params: {
token: '123456798',
name: 'avatar'
},
headers: {
smail: '*_~'
},
imgDataUrl: '' // the datebase64 url of created image
},
components: {
'my-upload': myUpload
},
methods: {
toggleShow() {
this.show = !this.show;
}
},
events: {
/**
* crop success
*
* [param] imgDataUrl
* [param] field
*/
cropSuccess(imgDataUrl, field){
console.log('-------- crop success --------');
this.imgDataUrl = imgDataUrl;
},
/**
* upload success
*
* [param] jsonData 服务器返回数据,已进行json转码
* [param] field
*/
cropUploadSuccess(jsonData, field){
console.log('-------- upload success --------');
console.log(jsonData);
console.log('field: ' + field);
},
/**
* upload fail
*
* [param] status server api return error status, like 500
* [param] field
*/
cropUploadFail(status, field){
console.log('-------- upload fail --------');
console.log(status);
console.log('field: ' + field);
}
}
});
</script>
写代码时遇到的问题
1.前后端都配好了 上传不进入控制器
打印前端上传错误,如下

最后定位在了mockjs这个文件 有冲突
所以进入package.json 删除mockjs 并重新install即可
2.要使用这个上传控制器 vue需要配置代理
如
config下
//图片代理访问
'/img':{
target:'http://localhost:8081/images',
changeOrigin:true,//允许跨域
pathRewrite: {
'^/img':''
}
},
若使用nginx部署 也需要添加代理配置
官网链接:
http://npm.taobao.org/package/vue-image-crop-upload-2
近期项目使用image源码:
1.效果如图: 鼠标悬浮 有个小特效 后面随即一个祝福成语 小彩蛋吧

首先是 小特效
<template>
<div :style="{zIndex:zIndex,height:height,width:width}" class="pan-item">
<div class="pan-info">
<div class="pan-info-roles-container">
<span style="font-size:10px;">{{blessing()}}</span>
<slot/>
</div>
</div>
<img :src="image" onerror="this.src='../../../static/img/default_portrait_128.png'" class="pan-thumb">
</div>
</template>
<script>
export default {
name: 'PanThumb',
props: {
image: {
type: String,
required: true,
},
zIndex: {
type: Number,
default: 1
},
width: {
type: String,
default: '150px'
},
height: {
type: String,
default: '150px'
}
},
methods:{
blessing(){
let text = parseInt(Math.random()*11);
console.log('text :', text);
let bless = '';
switch(text){
case 0: bless = '财源广进';break;
case 1: bless = '一帆风顺';break;
case 2: bless = '身体健康';break;
case 3: bless = '万事如意';break;
case 4: bless = '心想事成';break;
case 5: bless = '工作顺利';break;
case 6: bless = '幸福安康';break;
case 7: bless = '蒸蒸日上';break;
case 8: bless = '欣欣向荣';break;
case 9: bless = '恭喜发财';break;
case 10: bless = '福如东海';break;
}
return bless;
}
}
}
</script>
<style scoped>
.pan-item {
width: 200px;
height: 200px;
border-radius: 50%;
display: inline-block;
position: relative;
cursor: default;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}
.pan-info-roles-container {
padding: 20px;
text-align: center;
}
.pan-thumb {
width: 100%;
height: 100%;
background-size: 100%;
border-radius: 50%;
overflow: hidden;
position: absolute;
transform-origin: 95% 40%;
transition: all 0.3s ease-in-out;
}
.pan-thumb:after {
content: '';
width: 8px;
height: 8px;
position: absolute;
border-radius: 50%;
top: 40%;
left: 95%;
margin: -4px 0 0 -4px;
background: radial-gradient(ellipse at center, rgba(14, 14, 14, 1) 0%, rgba(125, 126, 125, 1) 100%);
box-shadow: 0 0 1px rgba(255, 255, 255, 0.9);
}
.pan-info {
position: absolute;
width: inherit;
height: inherit;
border-radius: 50%;
overflow: hidden;
box-shadow: inset 0 0 0 5px rgba(0, 0, 0, 0.05);
}
.pan-info h3 {
color: #fff;
text-transform: uppercase;
position: relative;
letter-spacing: 2px;
font-size: 18px;
margin: 0 60px;
padding: 22px 0 0 0;
height: 85px;
font-family: 'Open Sans', Arial, sans-serif;
text-shadow: 0 0 1px #fff, 0 1px 2px rgba(0, 0, 0, 0.3);
}
.pan-info p {
color: #fff;
padding: 10px 5px;
font-style: italic;
margin: 0 30px;
font-size: 12px;
border-top: 1px solid rgba(255, 255, 255, 0.5);
}
.pan-info p a {
display: block;
color: #333;
width: 80px;
height: 80px;
background: rgba(255, 255, 255, 0.3);
border-radius: 50%;
color: #fff;
font-style: normal;
font-weight: 700;
text-transform: uppercase;
font-size: 9px;
letter-spacing: 1px;
padding-top: 24px;
margin: 7px auto 0;
font-family: 'Open Sans', Arial, sans-serif;
opacity: 0;
transition: transform 0.3s ease-in-out 0.2s, opacity 0.3s ease-in-out 0.2s, background 0.2s linear 0s;
transform: translateX(60px) rotate(90deg);
}
.pan-info p a:hover {
background: rgba(255, 255, 255, 0.5);
}
.pan-item:hover .pan-thumb {
transform: rotate(-110deg);
}
.pan-item:hover .pan-info p a {
opacity: 1;
transform: translateX(0px) rotate(0deg);
}
</style>
其次 上传代码
<template>
<div>
<el-upload
ref="upload"
action="/file/uploadImgs"
list-type="picture-card"
:on-preview="handlePictureCardPreview"
:before-remove="handleRemove"
:on-success="handleSuccess"
:on-error="handleError"
:limit="imglimit"
:auto-upload="autoupload"
:file-list="filelist"
:on-exceed="handleExceed"
:before-upload="beforeUpload"
>
<i class="el-icon-plus"></i>
</el-upload>
<el-dialog append-to-body :visible.sync="dialogVisible">
<div style="z-index:999">
<img width="100%" :src="dialogImageUrl" alt="">
</div>
</el-dialog>
<el-button v-if="!autoupload" style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
</div>
</template>
<script>
export default {
props:{
fromfilelist:Array,
imglimit:Number,
autoupload:Boolean
},
data() {
return {
dialogImageUrl: '',
dialogVisible: false,
filelist:[],
uploadlist:new Set()
};
},
mounted(){
if(this.fromfilelist){
this.initFileList(this.fromfilelist);
}
},
methods: {
initFileList(val){
this.filelist = [];
this.filelist = Object.assign([],val);
this.filelist.map(item => {
this.uploadlist.add(item.name);
})
},
submitUpload() {
this.$refs.upload.submit();
},
beforeUpload(file){
var testmsg=file.name.substring(file.name.lastIndexOf('.')+1)
const extension = testmsg === 'jpg'
const extension2 = testmsg === 'png'
const isLt2M = file.size / 1024 / 1024 < 5 //这里做文件大小限制
if(!extension && !extension2) {
this.$message({
message: '上传文件只能是 jpg、png格式!',
type: 'warning'
});
}
if(!isLt2M) {
this.$message({
message: '上传文件大小不能超过 5MB!',
type: 'warning'
});
}
return extension || extension2 && isLt2M
},
handleError(err, file, fileList){
this.$message.error("文件上传失败,请稍后再试!");
},
handleExceed(files, fileList){
this.$message.error("文件超出个数限制,最大上传数为"+this.imglimit);
},
handleSuccess(response, file, fileList){
response.map(item => {
this.uploadlist.add(item);
})
},
handleRemove(file, fileList) {
let filename;
return this.$confirm(`确定移除 ${file.name}?`).then(()=>{
if(file.response){
filename = file.response[0];
}else{
filename = file.name;
}
let newname = filename;
let flag = this.fromfilelist.some(s => {
return s.name == newname.replace("-test","");
})
if(flag){
filename = filename.replace("-test","");
}
this.uploadlist.delete(filename);
})
},
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
}
}
}
</script>