0%

JS 点击按钮上传文件

通过点击按钮上传文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<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>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 = '';
});
}