1

我正在使用 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);
4

1 回答 1

0

您可以使用 el-upload 的 :before-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(); 

  }
 }
}
于 2018-03-16T05:18:20.987 回答