wails(v2.10.1) 框架界面编程,wails+ vue+ypescript 如何实现把文件拖入文本输入框,在输入框中显示文件路径?折腾了好久都搞不定。
<template>
<div
class="drop-zone"
@dragover.prevent="handleDragOver"
@drop.prevent="handleFileDrop"
>
<input
type="text"
v-model="filePath"
placeholder="拖拽文件到这里"
readonly
/>
<div v-if="fileName" class="file-preview">
<span>{{ fileName }}</span>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';
export default defineComponent({
setup() {
const filePath = ref("");
const fileName = ref("");
const handleDragOver = (e: DragEvent) => {
e.dataTransfer!.dropEffect = 'copy';
};
const handleFileDrop = (e: DragEvent) => {
const files = e.dataTransfer?.files
if (files && files.length > 0) {
const file = files[0] as File & { path: string };
fileName.value = file.name;
filePath.value = file.path; // Wails 扩展的 path 属性
if (filePath.value==null){filePath.value="空的"}
}
};
return { filePath, fileName, handleDragOver, handleFileDrop };
}
});
</script>
<style scoped>
.drop-zone {
border: 2px dashed #42b983;
padding: 20px;
text-align: center;
transition: all 0.3s;
}
.drop-zone:hover {
background-color: rgba(66, 185, 131, 0.1);
}
input {
width: 50%;
padding: 8px;
margin-bottom: 10px;
}
.file-preview {
margin-top: 10px;
color: #cdd0d4;
}
</style>
在main.go中,创建应用时,已设置窗口支持文件拖拽。
err := wails.Run(&options.App{
Title: "myproject",
Width: 1024,
Height: 768,
**//EnableDragAndDrop:true 网上搜了好多,都是这个参数,但是发现wails2.10.1中这个参数好像已经没有了,有以下参数**
DragAndDrop: &options.DragAndDrop{
EnableFileDrop: true,
DisableWebViewDrop: true,
},
**//EnableDragAndDrop**
AssetServer: &assetserver.Options{
Assets: assets,
},
BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
OnStartup: app.startup,
Bind: []interface{}{
app,
},
})