Vue获取上传的文件网址

时间:2018-03-16 04:49:07

标签: vue.js element-ui

我使用vue和element ui上传文件。但我不知道如何获取上传文件的网址(基本上我想检查文件类型 - jpg或pdf,然后做点什么)。

<div id="app">
<el-upload action="https://jsonplaceholder.typicode.com/posts/" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
<i class="el-icon-plus"></i>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
<embed width="100%" :src="dialogImageUrl">
</el-dialog>

<p>file type: "{{ checkType() }}"</p>
</div>

var Main = {
data() {
  return {
    dialogImageUrl: '',
    dialogVisible: false
  };
},
methods: {
  handleRemove(file, fileList) {
    console.log(file, fileList);
  },
  handlePictureCardPreview(file) {
    this.dialogImageUrl = file.url;
    this.dialogVisible = true;
  },
  checkType(){

     var filename =  this.dialogImageUrl;  
     return filename.split('.').pop(); 

  }
 }
}

请帮助......

我已添加此尝试以获取URL ..但它不起作用..

var filename = document.getElementsByTagName("embed")[0].src; 
console.log(filename);

1 个答案:

答案 0 :(得分:0)

你可以使用:el-upload的上传前属性。请看看改变后的代码。

<div id="app">
<el-upload action="https://jsonplaceholder.typicode.com/posts/" **:before-upload="checkType"** list-type="picture-card" :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
<i class="el-icon-plus"></i>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
<embed width="100%" :src="dialogImageUrl">
</el-dialog>

<p>file type: "{{ checkType() }}"</p>
</div>

var Main = {
data() {
  return {
    dialogImageUrl: '',
    dialogVisible: false
  };
},
methods: {
  handleRemove(file, fileList) {
    console.log(file, fileList);
  },
  handlePictureCardPreview(file) {
    this.dialogImageUrl = file.url;
    this.dialogVisible = true;
  },
  **checkType(file){**
    console.log(file.type);
    // do your action here based on file.type
     var filename =  this.dialogImageUrl;  
     return filename.split('.').pop(); 

  }
 }
}