Skip to content

JS 点击按钮上传文件

Published: at 07:45 PMSuggest Changes

通过点击按钮上传文件

<div class="flex items-center" @click="addImage">
  <img
    src="@/common/assets/Car/image.png"
    alt=""
    class="h-18px w-18px mr-10px"
  />
  <div class="text-10px font-pingfang font-400 text-333">图片</div>
  <input
    ref="imageFileUpload"
    type="file"
    hidden
    @change="onImageFileChanged"
    accept="image/png, image/gif, image/jpeg, image/bmp, image/x-icon"
    style="display: none"
  />
</div>
const imageFileUpload = ref<any>();

function addImage() {
  imageFileUpload.value.click();
}

function onImageFileChanged(params: any) {
  const formdata = new FormData();
  formdata.append('files', params.target.files[0]);
  imageFileUpload.value.value = '';
  uploadFile(formdata).then(res => {
    imageFileUpload.value.value = '';
  });
}

Previous Post
JS 创建 video 元素
Next Post
JS 自定义 Quill 富文本编辑器