el-upload上传文件回显

想要获取对应id所上传的附件文档,使我们在点击对应的商家时显示出他所上传的附件文档,并可以点击查看

有一个 file-list 属性
把你要显示的文件填充给这个对象
格式:[{name: 'food.jpg', url: 'https://xxx.cdn.com/xxx.jpg'}]

【以下回答由 GPT 生成】

问题:如何使用el-upload组件在vue.js中实现上传文件并回显?

解决方案: 1. 首先,确保已经正确安装了Vue.js和Element UI,并且在Vue组件中引入了el-upload组件。

  1. 在Vue组件中,声明一个data对象来存储上传的文件和文件列表。例如:
data() {
  return {
    fileList: [],
    uploadedFiles: []
  };
},
  1. 在template中使用el-upload组件并绑定相关属性和事件。例如:
<el-upload
  class="upload-demo"
  :action="uploadUrl"
  :file-list="fileList"
  :auto-upload="false"
  :on-change="handleChange"
>
  <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
  <el-button style="margin-left: 10px;" size="small" type="success" @click="handleUpload">上传文件</el-button>
</el-upload>
  1. 在Vue组件的methods中,实现handleChange方法来处理文件选择事件。例如:
handleChange(file, fileList) {
  this.fileList = fileList;
},
  1. 实现handleUpload方法来处理文件上传事件。在该方法中,可以通过FormData对象将文件数据上传到服务器,并接收服务器返回的文件列表。例如:
handleUpload() {
  const formData = new FormData();

  this.fileList.forEach(file => {
    formData.append('file', file.raw);
  });

  // 使用axios或其他网络库发送文件上传请求,并获取服务器返回的文件列表

  // 示例使用axios进行文件上传请求
  axios.post('/api/upload', formData).then(response => {
    // 上传成功后,将服务器返回的文件列表保存到uploadedFiles数组中
    this.uploadedFiles = response.data.files;
  }).catch(error => {
    console.log(error);
  });
},
  1. 在Vue组件中展示上传成功后的文件列表。例如:
<el-card>
  <div v-if="uploadedFiles.length > 0">
    <h3>已上传文件列表:</h3>
    <ul>
      <li v-for="file in uploadedFiles" :key="file.id">
        {{ file.name }}
        <el-button size="small" type="text" @click="handleView(file.url)">查看</el-button>
      </li>
    </ul>
  </div>
</el-card>
  1. 根据需求实现handleView方法来处理文件查看事件。例如:
handleView(url) {
  // 在新标签页或模态框中打开文件链接
  window.open(url);
},

以上就是使用el-upload组件在Vue.js中实现上传文件并回显的步骤。根据具体的情况,可以对代码进行适当的调整。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^