采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
为什么在老师这里上传文件之后,会存在一个路径path呢,正常好像是获取不到的,怎么才能获取到path呢
除了在页面用input框选择文件之外, 还可以直接让主线程调用electron提供的showDialog方法, 获取文件。然后返回path给前端就ok了
文档: https://www.electronjs.org/zh/docs/latest/api/dialog#dialogshowopendialogwindow-options
两位同学
找到原因了,最新版的 File 对象移除了 path这个属性:https://www.electronjs.org/docs/latest/api/file-object
推荐使用 webUtils.getPathForFile 获取对应的路径,替换为这个方法就可以了。
所以这里要简单修改一下:
// preload.ts 多加一个方法 getFilePath: (file: File) => webUtils.getPathForFile(file), // 注意定义文件也改一下 getFilePath: (file: File) => string; // 然后在MessageInput 中调用这个就可以 const onCreate = () => { if(model.value && model.value.trim() !== '') { if (selectedImage) { const filePath = window.electronAPI.getFilePath(selectedImage) emit('create', model.value, filePath) } else { emit('create', model.value) } selectedImage = null imagePreview.value = '' } }
收到,谢谢老师
同学你好
请问你使用的是什么系统?windows 还是 mac?有可能 windows 和 mac 在这里File 对象的属性不同,能否提供一下你的源代码(git),我先看下,然后看看 windows 要怎样处理下
是windows,仓库地址https://git.imooc.com/anzaigo/v-chat.git
老师,我mac系统也是这样的 files里没有path属性,请问一下这个怎么解决?
登录后可查看更多问答,登录/注册
文心一言/通义千问/Deepseek/ Claude/Cursor与Vue3、Electron
602 8
514 7
184 7
338 6
517 6
购课补贴联系客服咨询优惠详情
慕课网APP您的移动学习伙伴
扫描二维码关注慕课网微信公众号