小demo演示一下vuevue-cropper的使用方法

  最近自己在研究vue,然后做了一个小型的后台管理系统用来练手,开发过程中,想到了剪切图片上传用户头像的需求。上网百度了一番js上传图片到服务器,发现好多用的都是vue-cropper。我也就用了,个人感觉还是挺好用的。现在在这里用一个简单的小demo演示一下vue-cropper的使用方法。

  其中上传用户头像的接口是java写的,感兴趣的话可以参考我的2017年12月2号的博客:前后端分离跨服务器文件上传-Java SpringMVC版

  1、安装vue-cropper

  使用npm本地安装vue-cropper

  npm install vue-cropper --save-dev

  2、新建一个test.vue文件

  该文件只做用来演示剪切上传图片的功能,下面直接贴出代码

  test.vue:

  

  1. ![头像][1]
  2. 选择图片
  3. ![js上传图片到项目服务器上_js上传图片到阿里云_js上传图片到服务器][2]
  4. import VueCropper from 'vue-cropper'
  5. import Api from '@/js/api.js' //接口url配置文件
  6. export default {
  7. data() {
  8. return {
  9. headImg:'',
  10. //剪切图片上传
  11. crap: false,
  12. previews: {},
  13. option: {
  14. img: '',
  15. outputSize:1, //剪切后的图片质量(0.1-1
  16. full: false,//输出原图比例截图 props名full
  17. outputType: 'png',
  18. canMove: true,
  19. original: false,
  20. canMoveBox: true,
  21. autoCrop: true,
  22. autoCropWidth: 150,
  23. autoCropHeight: 150,
  24. fixedBox: true
  25. },
  26. fileName:'', //本机文件地址
  27. downImg: '#',
  28. imgFile:'',
  29. uploadImgRelaPath:'', //上传后的图片的地址(不带服务器域名)
  30. }
  31. },
  32. components: {
  33. VueCropper
  34. },
  35. methods: {
  36. //放大/缩小
  37. changeScale(num) {
  38. console.log('changeScale')
  39. num = num || 1;
  40. this.$refs.cropper.changeScale(num);
  41. },
  42. //坐旋转
  43. rotateLeft() {
  44. console.log('rotateLeft')
  45. this.$refs.cropper.rotateLeft();
  46. },
  47. //右旋转
  48. rotateRight() {
  49. console.log('rotateRight')
  50. this.$refs.cropper.rotateRight();
  51. },
  52. //上传图片(点击上传按钮)
  53. finish(type) {
  54. console.log('finish')
  55. let _this = this;
  56. let formData = new FormData();
  57. // 输出
  58. if (type === 'blob') {
  59. this.$refs.cropper.getCropBlob((data) => {
  60. let img = window.URL.createObjectURL(data)
  61. this.model = true;
  62. this.modelSrc = img;
  63. formData.append("file", data, this.fileName);
  64. this.$http.post(Api.uploadSysHeadImg.url, formData, {contentType: false, processData: false, headers:{'Content-Type': 'application/x-www-form-urlencoded'}})
  65. .then((response)=>{
  66. var res = response.data;
  67. if(res.success == 1){
  68. $('#btn1').val('');
  69. _this.imgFile = '';
  70. _this.headImg = res.realPathList[0]; //完整路径
  71. _this.uploadImgRelaPath = res.relaPathList[0]; //非完整路径
  72. _this.$message({  //element-ui的消息Message消息提示组件
  73. type: 'success',
  74. message: '上传成功'
  75. });
  76. }
  77. })
  78. })
  79. } else {
  80. this.$refs.cropper.getCropData((data) => {
  81. this.model = true;
  82. this.modelSrc = data;
  83. })
  84. }
  85. },
  86. // 实时预览函数
  87. realTime(data) {
  88. console.log('realTime')
  89. this.previews = data
  90. },
  91. //下载图片
  92. down(type) {
  93. console.log('down')
  94. var aLink = document.createElement('a')
  95. aLink.download = 'author-img'
  96. if (type === 'blob') {
  97. this.$refs.cropper.getCropBlob((data) => {
  98. this.downImg = window.URL.createObjectURL(data)
  99. aLink.href = window.URL.createObjectURL(data)
  100. aLink.click()
  101. })
  102. } else {
  103. this.$refs.cropper.getCropData((data) => {
  104. this.downImg = data;
  105. aLink.href = data;
  106. aLink.click()
  107. })
  108. }
  109. },
  110. //选择本地图片
  111. uploadImg(e, num) {
  112. console.log('uploadImg');
  113. var _this = this;
  114. //上传图片
  115. var file = e.target.files[0]
  116. _this.fileName = file.name;
  117. if (!/.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
  118. alert('图片类型必须是.gif,jpeg,jpg,png,bmp中的一种')
  119. return false
  120. }
  121. var reader = new FileReader();
  122. reader.onload =(e) => {
  123. let data;
  124. if (typeof e.target.result === 'object') {
  125. // 把Array Buffer转化为blob 如果是base64不需要
  126. data = window.URL.createObjectURL(new Blob([e.target.result]))
  127. }
  128. else {
  129. data = e.target.result
  130. }
  131. if (num === 1) {
  132. _this.option.img = data
  133. } else if (num === 2) {
  134. _this.example2.img = data
  135. }
  136. }
  137. // 转化为base64
  138. // reader.readAsDataURL(file)
  139. // 转化为blob
  140. reader.readAsArrayBuffer(file);
  141. },
  142. imgLoad (msg) {
  143. console.log('imgLoad')
  144. console.log(msg)
  145. }
  146. },
  147. }
  148. .info {
  149. width: 720px;
  150. margin: 0 auto;
  151. .oper-dv {
  152. height:20px;
  153. text-align:right;
  154. margin-right:100px;
  155. a {
  156. font-weight: 500;
  157. &:last-child {
  158. margin-left: 30px;
  159. }
  160. }
  161. }
  162. .info-item {
  163. margin-top: 15px;
  164. label {
  165. display: inline-block;
  166. width: 100px;
  167. text-align: right;
  168. }
  169. .sel-img-dv {
  170. position: relative;
  171. .sel-file {
  172. position: absolute;
  173. width: 90px;
  174. height: 30px;
  175. opacity: 0;
  176. cursor: pointer;
  177. z-index: 2;
  178. }
  179. .sel-btn {
  180. position: absolute;
  181. cursor: pointer;
  182. z-index: 1;
  183. }
  184. }
  185. }
  186. }
  187. .cropper-content{
  188. display: flex;
  189. display: -webkit-flex;
  190. justify-content: flex-end;
  191. -webkit-justify-content: flex-end;
  192. .cropper{
  193. width: 260px;
  194. height: 260px;
  195. }
  196. .show-preview{
  197. flex: 1;
  198. -webkit-flex: 1;
  199. display: flex;
  200. display: -webkit-flex;
  201. justify-content: center;
  202. -webkit-justify-content: center;
  203. .preview{
  204. overflow: hidden;
  205. border-radius: 50%;
  206. border:1px solid #cccccc;
  207. background: #cccccc;
  208. margin-left: 40px;
  209. }
  210. }
  211. }
  212. .cropper-content .show-preview .preview {margin-left: 0;}

  其中,js/api.js文件是配置的接口地址

  3、效果

  1、打开页面效果

  2、点击选择图片按钮,选择完本地图片后的效果

  选择完图片后,就可以对图片进行放大,缩小以及旋转等,并且可以移动选中框,选择上传图片的任意部分

  3、点击上传头像按钮,即可调用上传头像的接口js上传图片到服务器,把头像上传到文件服务器

  此时,图片便已上传成功了,查看图片服务器指定的目录,即可查看到图片已经在服务器上了

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1529
0 评论
919

发表评论

!