• 【vue-upload】表单中自定义头像上传,或手动上传


    一、自定义上传

    使用http-request实现自定义上传 

    1. <el-form-item prop="imageUrl" label="二维码:">
    2. <el-upload
    3. class="avatar-uploader"
    4. ref="er"
    5. action="#"
    6. :show-file-list="false"
    7. :http-request="Upload"
    8. :on-change="
    9. (file, fileList) => {
    10. change(file, fileList)
    11. }
    12. "
    13. >
    14. <img v-if="Form.imageUrl" :src="Form.imageUrl" class="avatar" />
    15. <i v-else class="el-icon-plus avatar-uploader-icon">i>
    16. el-upload>
    17. el-form-item>

    注意:使用自定义上传,on-success无效,所以改用on-change 

    1. export default {
    2. name: "Form",
    3. data () {
    4. return {
    5. Form: {
    6. imageUrl: '',//二维码
    7. },
    8. formData: new FormData(),
    9. file: {},//二维码
    10. };
    11. },
    12. created () {
    13. this.getform()
    14. },
    15. methods: {
    16. // 更新二维码
    17. Upload (param) {
    18. this.formData.append("file", param.file);
    19. upcode(this.formData).then((res) => {
    20. console.log(res)
    21. this.Form.imageUrl = res.data
    22. // this.Form.imageUrl = URL.createObjectURL(param.file)
    23. this.$message.success(res.message);
    24. });
    25. },
    26. change (file, fileList) {
    27. const isJPEG = file.raw.type === 'image/jpeg'
    28. const isPNG = file.raw.type === 'image/png'
    29. const isJPG = file.raw.type === 'image/jpg'
    30. const isGIF = file.raw.type === 'image/GIF'
    31. const isLt2M = file.size / 1024 / 1024 < 10;
    32. if (!(isJPEG || isPNG || isJPG || isGIF)) {
    33. this.$message.error('上传图片只能是 JPEG/PNG/JPG/GIF 格式!')
    34. } else if (!isLt2M) {
    35. this.$message.error("上传图片大小不能超过 10MB!");
    36. }
    37. if (fileList.length > 1) {
    38. fileList.splice(0, 1)
    39. }
    40. },
    41. }
    42. }

    二、手动上传

    使用auto-upload取消立即上传 

    1. <el-form-item prop="image9" label="拼图9:">
    2. <el-upload
    3. class="avatar-uploader"
    4. ref="image9"
    5. action="#"
    6. :show-file-list="false"
    7. :auto-upload="false"
    8. :on-change="
    9. (file, fileList) => {
    10. change9(file, fileList)
    11. }
    12. "
    13. >
    14. <img v-if="Form.image9" :src="Form.image9" class="avatar" />
    15. <i v-else class="el-icon-plus avatar-uploader-icon">i>
    16. el-upload>
    17. el-form-item>
    18. <el-form-item>
    19. <el-button type="primary" @click="onSubmit">提交el-button>
    20. <el-button type="primary" @click="cancel">重置el-button>
    21. el-form-item>
    1. export default {
    2. name: "Form",
    3. data () {
    4. return {
    5. Form: {
    6. image9: '',//二维码
    7. },
    8. formData: new FormData(),
    9. file9: {},//二维码
    10. };
    11. },
    12. created () {
    13. this.getform()
    14. },
    15. methods: {
    16. // 碎片图片9
    17. change9 (file, fileList) {
    18. const isJPEG = file.raw.type === 'image/jpeg'
    19. const isPNG = file.raw.type === 'image/png'
    20. const isJPG = file.raw.type === 'image/jpg'
    21. const isGIF = file.raw.type === 'image/GIF'
    22. const isLt2M = file.size / 1024 / 1024 < 10;
    23. if (!(isJPEG || isPNG || isJPG || isGIF)) {
    24. this.$message.error('上传图片只能是 JPEG/PNG/JPG/GIF 格式!')
    25. } else if (!isLt2M) {
    26. this.$message.error("上传图片大小不能超过 10MB!");
    27. }
    28. if (fileList.length > 1) {
    29. fileList.splice(0, 1)
    30. }
    31. // console.log(file);
    32. if (file.status === "ready") {
    33. this.Form.image9 = URL.createObjectURL(file.raw)
    34. this.file9 = file.raw
    35. }
    36. },
    37. // 提交
    38. async onSubmit () {
    39. this.$refs.Form.validate(async (isok) => {
    40. if (isok) {
    41. // 提交编辑
    42. // console.log(this.Form);
    43. const res = await setTime(this.Form);//时间
    44. console.log(res)
    45. // 拼图9
    46. this.formData.append("file", this.file9);
    47. upcode(this.formData).then((res) => {
    48. console.log(res)
    49. this.Form.image9 = res.data
    50. });
    51. this.$message.success(res.message);
    52. }
    53. // this.DetailTaskList();
    54. });
    55. },
    56. }
    57. }

  • 相关阅读:
    Linux 生成复杂密码并且检查密码强度
    【面试经典150 | 区间】用最少数量的箭引爆气球
    夏洛克和他的女朋友—线性筛—逻辑
    .NET、VUE利用RSA加密完成登录并且发放JWT令牌设置权限访问
    vue学习第五天(9月8号)
    Oracle常用函数大全
    C++中 while循环和for循环优缺点
    Redis八股文(大厂面试真题)
    分治算法详解
    尝试了一个自然语言模型BLOOM
  • 原文地址:https://blog.csdn.net/Qxn530/article/details/127744820