Home Web Front-end JS Tutorial In Tauri Vue3 application: How to correctly parse local binary image files?

In Tauri Vue3 application: How to correctly parse local binary image files?

Apr 04, 2025 am 11:27 AM
vue Browser tool ai

In Tauri Vue3 application: How to correctly parse local binary image files?

Solutions to parse local binary image files in Tauri and Vue3 applications

This article discusses the problem that local images cannot be displayed due to security policy restrictions in the Markdown parser built with Tauri, Vue3, and markdown-it, and provides a final solution.

Problem: This tool uses v-html to render images (relative paths) in a Markdown file. The image is displayed normally in development mode, but after construction, the browser security policy still prevents the front-end from accessing local images even if the path is converted to an absolute path (for example: https://tauri.localhost/img/xxx.png ).

Try: Modifying the CSP policy in tauri.conf.json (for example, setting img-src to * ) is invalid. Two solutions were tried: 1. Use Tauri's fs API to read local files; 2. Read binary files in the Rust backend, pass data to the frontend, and parse using Blob object. Both solutions cause the image display to be corrupt or unloadable. The suspect is the image parsing or data format of Vue.js.

The initial front-end code tries to convert the binary data into a string, then encodes it as base64 using window.btoa , and finally sets src attribute of img element.

Final solution: Generate URL using Blob object and URL.createObjectURL method. The code is as follows:

 let img_path = "E:/myProjects2/tauri_vue/mdren/img/a-1-01.png";
const contents = await readBinaryFile(img_path); // Assume that the readBinaryFile function has been defined let blob = new Blob([contents], { type: "image/png" });

async function reloadImg() {
  let img_element = document.querySelectorAll("#img-to-remove");
  img_element.forEach(async (element) => {
    element.src = URL.createObjectURL(blob);
  });
}
Copy after login

Cause analysis: The previous solution failed may be due to CSP configuration errors or Rust backend data delivery issues. Use the Blob object and URL.createObjectURL to directly create the image URL, bypassing security policy restrictions and successfully resolve the image display problem.

The above is the detailed content of In Tauri Vue3 application: How to correctly parse local binary image files?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue and Element-UI cascaded drop-down box props pass value Vue and Element-UI cascaded drop-down box props pass value Apr 07, 2025 pm 07:36 PM

The data structure must be clearly defined when the Vue and Element-UI cascaded drop-down boxes pass the props, and the direct assignment of static data is supported. If data is dynamically obtained, it is recommended to assign values ​​within the life cycle hook and handle asynchronous situations. For non-standard data structures, defaultProps or convert data formats need to be modified. Keep the code simple and easy to understand with meaningful variable names and comments. To optimize performance, virtual scrolling or lazy loading techniques can be used.

How to use export default and import in Vue How to use export default and import in Vue Apr 07, 2025 pm 07:09 PM

export default is used to export Vue components and allow other modules to access. import is used to import components from other modules, which can import a single or multiple components.

How to use export default in Vue How to use export default in Vue Apr 07, 2025 pm 07:21 PM

Export default in Vue reveals: Default export, import the entire module at one time, without specifying a name. Components are converted into modules at compile time, and available modules are packaged through the build tool. It can be combined with named exports and export other content, such as constants or functions. Frequently asked questions include circular dependencies, path errors, and build errors, requiring careful examination of the code and import statements. Best practices include code segmentation, readability, and component reuse.

How to implement Vue and Element-UI cascade selector How to implement Vue and Element-UI cascade selector Apr 07, 2025 pm 08:18 PM

Vue and Element-UI cascade selectors can directly use the el-cascader component in simple scenarios, but to write more elegant, efficient and robust code, you need to pay attention to the following details: Data source structure optimization: Flatten the data and use id and parentId to represent the parent-child relationship. Asynchronous loading data processing: handles loading status, error prompts and user experience. Performance optimization: Consider on-demand loading or virtual scrolling technology. Code readability and maintainability: Write comments, use meaningful variable names, and follow code specifications.

Is the request method (GET, POST, etc.) used correctly? Is the request method (GET, POST, etc.) used correctly? Apr 07, 2025 pm 10:09 PM

The use of Axios request method in Vue.js requires following these principles: GET: Obtain resources, do not modify data. POST: Create or submit data, add or modify data. PUT: Update or replace existing resources. DELETE: Delete the resource from the server.

Why can't index be used in vue Why can't index be used in vue Apr 07, 2025 pm 11:09 PM

It is inappropriate to use an index as the key of a Vue list because: the index changes as elements are added or removed. There may be elements in the array with the same index. Reordering the list changes the index.

Why do Vue components use export default Why do Vue components use export default Apr 07, 2025 pm 07:06 PM

Beginners prefer exporting to Vue components because it simplifies component export, improves flexibility, avoids naming conflicts, and is specially handled in build tools, helping to optimize build efficiency. In addition, it improves the readability and maintainability of the code and reduces the possibility of errors.

What is the difference between export default and export in Vue What is the difference between export default and export in Vue Apr 07, 2025 pm 07:12 PM

The difference between export default and export: export allows name export. The same name is required when importing, and multiple components can be exported, which is clear and easy to maintain. export default allows exporting only one default value, simplifies import but reduces clarity, which can easily lead to naming conflicts in large projects. It is recommended to use export first, unless you are sure that you only need to export one component.

See all articles