Electron与Vite和Vue 3打开自定义文本文件

huangapple go评论49阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年5月10日 15:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定