首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何打开上传文件的浏览文件夹?

如何打开上传文件的浏览文件夹?
EN

Stack Overflow用户
提问于 2022-01-27 12:43:02
回答 1查看 176关注 0票数 0

我有视频上传组件。我需要显示上传的图像,并显示在每个图像按钮上传新的图像。据我所知,这个插件没有修改上传文件的方法。所以我想用手工来做。但我不知道怎么打开文件夹来选择文件。

如何打开浏览文件夹以获取新图像?

代码语言:javascript
复制
</template>
<div>
 <file-upload
   :ref="uploaderName"
   v-model="files"
   :input-id="uploaderName"
   :extensions="formats"
   :drop="drop"
   :multiple="multiple"
   @input-filter="inputFilter"
   >
    Upload a file
 </file-upload>
 <span>or drag and drop</span>
</div>

<div
 v-show="files.length && !loading"
 class="flex"
>
 <img
   v-for="image in files"
   :key="image.id"
   :src="image.url"
   :style="[imageSize]"
   class="uploaded-image p-2 mr-2 border rounded border-gray-200"
  >
  <button @click.prevent='() => uploadNew(image)'>
    Upload new
  </button>
  <button @click.prevent='() => removeFile(image)'>
    Remove
  </button>
</div>
</template>

<script>
import FileUpload from 'vue-upload-component';

export default {
  name: 'UploadFileForm',
  components: {
    FileUpload,
  },
  props: {
    uploaderName: {
      type: String,
      required: true,
      default: '',
    },
    formats: {
      type: Array,
      default: () => ['.jpg', '.jpeg', '.svg', '.png', '.webp'],
    },
    multiple: {
      type: Boolean,
      default: true,
    },
    drop: {
      type: Boolean,
      default: true,
    },
    imageWidth: {
      type: Number,
    },
    imageHeight: {
      type: Number,
    },
    value: {
      type: Array,
      default: () => {},
    },
  },
  data() {
    return {
      files: this.value || [],
      error: '',
      loading: false,
      hover: false,
      minImageWidth: 372,
      minImageHeight: 300,
    };
  },
methods: {
    removeFile(file) {
      this.$refs[this.uploaderName].remove(file);
    },
    uploadNew() {
      
    }
};
</script>
EN

回答 1

Stack Overflow用户

发布于 2022-01-27 14:09:32

这样做有不同的方法。一个是创建一个区域,您可以选择一个文件上传或拖放上传。

代码语言:javascript
复制
<template>
  <div class="file-upload">
    <div
      class="text_field"
      @click="pickFile"
      @drop="uploadFile"
      @dragover.prevent
      @drop.prevent
    >
      <input
        id="file"
        ref="image"
        :accept="allowedFileTypes"
        style="display: none"
        type="file"
        @change="uploadFile"
      />
      <span v-if="fileUploading"> File uploading </span>
      <span v-else> Drag file here or click to upload </span>
    </div>
  </div>
</template>

脚本

代码语言:javascript
复制
<script>
export default {
  name: "UploadFile",
  data() {
    return {
      fileUploading: false,
      allowedFileTypes: ".pdf, .jpg, .jpeg, .png, .doc, .docx, .xls, .xlsx, video/*, audio/*",
    };
  },
  methods: {
    pickFile() {
      this.$refs.image.click();
    },
    async uploadFile(e) {
      this.fileUploading = true;
      const files = e.target.files || e.dataTransfer.files;
      if (!files.length) {
        return;
      }
      const file = document.getElementById("file").files[0];
      /* Creating a form element so that it can be detected as req.files (in Node.js, for example) */
      const fileForm = new window.FormData();
      fileForm.append("file", file);
      /* Simulating uploading of files, we wait for two seconds */
      setTimeout(() => {
        this.fileUploading = false;
      }, 2000);
      /* Below, you can make a request to your backend to post the image */
      /* await axios.post('your_upload_file_url', fileForm)
        .then(res => res)
        .catch((error) => Promise.reject(error))
        */
    },
  },
};
</script>

您也可以添加一些样式。

代码语言:javascript
复制
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.file-upload {
  border: 1px dashed #007991FF;
  border-radius: 5px;
  height: 192px;
  cursor: pointer;
  text-align: center;
  vertical-align: middle;
  span {
    position: relative;
    top: 75px;
    padding: 20px;
    font-size: 14px;
    color: #cac8c8;
  }
}
</style>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70878739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档