


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); }); }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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.

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.

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.

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.

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.

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.
