当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Vue + Element UI图片上传控件使用详解

Vue + Element UI图片上传控件使用详解

2019年09月06日  | 移动技术网IT编程  | 我要评论

上一篇 vue +element ui +vue-quill-editor 富文本编辑器及插入图片自定义 主要是写了富文本编辑器的自定义及编辑器中图片的上传并插入到编辑内容,这篇文章单独介绍一下element ui 图片上传控件的使用。首先要安装element并中引入,安装引入过程这里不再赘述。

1.引用element 上传控件。

<el-upload
 action="/mgr/common/imgupload"//这里需要配置一下文件上传地址(跨域)
 list-type="picture-card"
 accept="image/*"
 :limit="imglimit"
 :file-list="productimgs"
 :multiple="ismultiple"
 :on-preview="handlepicturecardpreview"
 :on-remove="handleremove"
 :on-success="handleavatarsuccess"
 :before-upload="beforeavatarupload"
 :on-exceed="handleexceed"
 :on-error="imguploaderror">
 <i class="el-icon-plus"></i>
 </el-upload>
 <el-dialog :visible.sync="dialogvisible">
 <img width="100%" :src="dialogimageurl" alt="">
 </el-dialog>

2.js

export default {
 data() {
 return {
 dialogimageurl: '',
 dialogvisible: false,
 productimgs: [],
 ismultiple: true,
 imglimit: 6
 }
 },
 methods: {
 handleremove(file, filelist) {//移除图片
 console.log(file, filelist);
 },
 handlepicturecardpreview(file) {//预览图片时调用
 console.log(file);
 this.dialogimageurl = file.url;
 this.dialogvisible = true;
 },
 
 beforeavatarupload(file) {//文件上传之前调用做一些拦截限制
 console.log(file);
 const isjpg = true;
 // const isjpg = file.type === 'image/jpeg';
 const islt2m = file.size / 1024 / 1024 < 2;
 
 // if (!isjpg) {
 // this.$message.error('上传头像图片只能是 jpg 格式!');
 // }
 if (!islt2m) {
  this.$message.error('上传图片大小不能超过 2mb!');
 }
 return isjpg && islt2m;
 },
 handleavatarsuccess(res, file) {//图片上传成功
 console.log(res);
 console.log(file);
 this.imageurl = url.createobjecturl(file.raw);
 },
 handleexceed(files, filelist) {//图片上传超过数量限制
 this.$message.error('上传图片不能超过6张!');
 console.log(file, filelist);
 },
 imguploaderror(err, file, filelist){//图片上传失败调用
 console.log(err)
 this.$message.error('上传图片失败!');
 }
 }
 }

3.controller

 @requestmapping(value = "/imgupload")
 public wrapper imgupload(httpservletrequest req, multiparthttpservletrequest multireq)
  throws ioexception {
 system.out.println("---" + fileuploadpath);//我这里用的springboot 在application.properties中配置,使用@value 获取的文件上传目录
 
 multipartfile file = multireq.getfile("file");
 string originalfilename = file.getoriginalfilename();
 string suffix = originalfilename.substring(originalfilename.indexof("."));
 string localfilename = md5util.md5(file.getinputstream()) + suffix;
 file localfile = new file(fileuploadpath + localfilename);
 if (!localfile.exists()) {
  localfile.createnewfile();
 
  fileoutputstream fos = new fileoutputstream(
   localfile);
  fileinputstream fs = (fileinputstream) multireq.getfile("img").getinputstream();
  byte[] buffer = new byte[1024];
  int len = 0;
  while ((len = fs.read(buffer)) != -1) {
  fos.write(buffer, 0, len);
  }
  fos.close();
  fs.close();
 
 } else {
  log.info("文件已存在!!");
 }
 
 return wrapmapper.wrap(
  wrapper.success_code,
  wrapper.success_message,
  "http://localhost:8080/img/" + localfilename);//这里是我执行封装的返回结果,也可以使用map,
 }

4.md5工具类

import java.io.*;
import java.security.messagedigest;
import java.security.nosuchalgorithmexception;
 
public class md5util {
 
 private static char[] digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8',
  '9', 'a', 'b', 'c', 'd', 'e', 'f'};
 
 public static string getmd5sum(string inputstr)
  throws nosuchalgorithmexception {
 messagedigest digest = messagedigest.getinstance("md5");
 byte[] inputstrbyte = inputstr.getbytes();
 digest.update(inputstrbyte, 0, inputstrbyte.length);
 
 byte[] md5sum = digest.digest();
 
 stringbuffer sb = new stringbuffer();
 for (int i = 0; i < 16; i++) {
  char[] ob = new char[2];
  ob[0] = digit[md5sum[i] >> 4 & 0x0f];
  ob[1] = digit[md5sum[i] & 0x0f];
  string s = new string(ob);
  sb.append(s);
 }
 
 return sb.tostring();
 }
 
 /**
 * 对字符串进行 md5 加密
 *
 * @param str
 *  待加密字符串
 *
 * @return 加密后字符串
 */
 public static string md5(string str) {
 messagedigest md5 = null;
 try {
  md5 = messagedigest.getinstance("md5");
  md5.update(str.getbytes("utf-8"));
 } catch (nosuchalgorithmexception e) {
  throw new runtimeexception(e.getmessage());
 } catch (unsupportedencodingexception e) {
  throw new runtimeexception(e.getmessage());
 }
 byte[] encodedvalue = md5.digest();
 int j = encodedvalue.length;
 char finalvalue[] = new char[j * 2];
 int k = 0;
 for (int i = 0; i < j; i++) {
  byte encoded = encodedvalue[i];
  finalvalue[k++] = digit[encoded >> 4 & 0xf];
  finalvalue[k++] = digit[encoded & 0xf];
 }
 
 return new string(finalvalue);
 }
 
 /**
 * 签名字符串
 *
 * @param text
 *  需要签名的字符串
 * @param sign
 *  签名结果
 * @return 签名结果
 */
 public static boolean verify(string text, string sign) {
 string mysign = md5(text);
 if (mysign.equals(sign)) {
  return true;
 } else {
  return false;
 }
 }
 
 /**
 * 对文件进行 md5 加密
 *
 * @param file
 *  待加密的文件
 *
 * @return 文件加密后的 md5 值
 * @throws ioexception
 */
 public static string md5(file file) throws ioexception {
 fileinputstream is = new fileinputstream(file);
 return md5(is);
 
 }
 
 
 public static string md5(inputstream inputstream) throws ioexception {
 
 messagedigest md5 = null;
 try {
  md5 = messagedigest.getinstance("md5");
  int n = 0;
  byte[] buffer = new byte[1024];
  do {
  n = inputstream.read(buffer);
  if (n > 0) {
   md5.update(buffer, 0, n);
  }
  } while (n != -1);
  inputstream.skip(0);
 } catch (nosuchalgorithmexception e) {
  throw new runtimeexception(e.getmessage());
 } finally {
  inputstream.close();
 }
 
 byte[] encodedvalue = md5.digest();
 
 int j = encodedvalue.length;
 char finalvalue[] = new char[j * 2];
 int k = 0;
 for (int i = 0; i < j; i++) {
  byte encoded = encodedvalue[i];
  finalvalue[k++] = digit[encoded >> 4 & 0xf];
  finalvalue[k++] = digit[encoded & 0xf];
 }
 return new string(finalvalue);
 }
}

5.效果

6.主要参考文档 element 官方中文文档,文档中好多属性介绍很笼统不够详细,个人感觉比较坑。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网