英文:
Electron with Vite and Vue 3 open custom file txt
问题
I added to my project folder custom-resources
.
custom-resources
is in the same location as the package.json
file.
Next, I add to electron-builder.json5
file: extraResources: ['custom-resources'],
Now I tried to read file txt from the custom-resources folder. So I added:
Function to create a path:
export const MCRA_RESOURCES_PATH = IS_DEV ? __dirname : process.resourcesPath
export const MCRA_RESOURCES_CATALOG_NAME = IS_DEV ? '../custom-resources' : 'custom-resources'
And function to read the file:
export const getFile = (file: string) => {
return path.join(MCRA_RESOURCES_PATH, MCRA_RESOURCES_CATALOG_NAME, file)
}
When I build and install my application, reading the file txt works correctly. But I have a problem with the development version.
In localhost I have the path:
/Users/michal/projects/exampleApp/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/custom-resources/test.txt
And I got error 404 (Not Found). I don't have electron.asar
in my project.
How can I open the file in localhost?
英文:
I added to my project folder custom-resources
.
custom-resources
is in the same location as the package.json
file.
Next I add to electron-builder.json5
file: extraResources: ['custom-resources'],
Now I tried to read file txt from custom-resources folder. So I added:
Function to create path:
export const MCRA_RESOURCES_PATH = IS_DEV ? __dirname : process.resourcesPath
export const MCRA_RESOURCES_CATALOG_NAME = IS_DEV ? '../custom-resources' : 'custom-resources'
And function to read file:
export const getFile = (file: string) => {
return path.join(MCRA_RESOURCES_PATH, MCRA_RESOURCES_CATALOG_NAME, file)
}
When I build and install my application reading of file txt works correctly. But I have an problem with develop version.
In localhost I have path:
/Users/michal/projects/exampleApp/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/custom-resources/test.txt
And I got error 404 (Not Found). I don't have electron.asar
in my project
How I can open file in localhost?
答案1
得分: 0
我将这部分代码更改为:
```lang-js
const BASE_URL = import.meta.env.BASE_URL
export const MCRA_RESOURCES_PATH = IS_DEV ? BASE_URL : process.resourcesPath
现在文件地址是:/custom-resources/test.txt
。正常工作。
<details>
<summary>英文:</summary>
I change this:
```lang-js
export const MCRA_RESOURCES_PATH = IS_DEV ? __dirname : process.resourcesPath
to this:
const BASE_URL = import.meta.env.BASE_URL
export const MCRA_RESOURCES_PATH = IS_DEV ? BASE_URL : process.resourcesPath
And now address to file is: /custom-resources/test.txt
. Works correctly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论