img-upload.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. Vue.component('img-upload', {
  2. template: '<div class="fl mr15 mt20">\
  3. <input ref="imgFile" type="file" name="FileUpload" class="file-upload" @change="addFile">\
  4. <img src="../../../images/tianjiatupian_btn.png" width="65">\
  5. </div>',
  6. props: [],
  7. data: function() {
  8. return {
  9. }
  10. },
  11. methods: {
  12. addFile: function() {
  13. var vm = this
  14. var file = vm.$refs.imgFile.files[0]
  15. if(file && file.size) {
  16. var formFile = new FormData()
  17. formFile.append("file", file)
  18. httpRequest.post('upload/fastDFSImag',{
  19. data: formFile,
  20. cache: false,//上传文件无需缓存
  21. processData: false,//用于对data参数进行序列化处理 这里必须false
  22. contentType: false, //必须
  23. }).then(function(res) {
  24. if(res.status == 200) {
  25. vm.$emit('uploaded', res.data)
  26. } else {
  27. toastr && toastr.error(res.msg || "上传失败")
  28. }
  29. }).catch(function(e) {
  30. console.error(e)
  31. })
  32. }
  33. }
  34. }
  35. })