How to load a binary file from the server filesystem into Vite/Vue3? #9065
-
I would like to load (and process) data that is stored in a binary file on the server. Vue.app
... results in:
What is the way? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
Use File API in the browser. |
Beta Was this translation helpful? Give feedback.
-
Client-side render mechanism means it cannot load server-side files directly. Maybe you can load it as an asset:
|
Beta Was this translation helpful? Give feedback.
-
The simple answer is you can't load binary file data into a web app directly via JavaScript since the security data model in browsers will block you. File I/O is very restricted otherwise a site/app could attempt to grab files on a user's system. If the data file is text/json you can import the file like so: To process a binary file you would need to setup a small app server. You could do this in JavaScript using Node/Express or in Python I would probably use FastAPI module. Then you can use fetch() to call your REST endpoint to process the file and return a status such as success or failure or extract some data and return it as part of the REST response. If it is a real production app you need to take into consideration security and protect your endpoint using something like JWT tokens otherwise the endpoint is exposed to the public and anyone can call it and manipulate the user's files. |
Beta Was this translation helpful? Give feedback.
-
Hi, I am assuming you have created a new Vue3 project, which uses Vite. In which case, you are able to make use of the Asset API to load your binary file. The previous answers are right in saying there's not a quick and easy way to access the server file system from a Vue component/web page, since the Vue3 Vite template makes use of purely client side rendering (example: your site is built into static HTML/CSS/JS files). Therefore, the most optimal solution would be to instruct the bundler to include your binary file, using the Asset API mentioned above. Hope this helps, I'll attach a demo when I get to my computer. 😃 Update: demo link https://codesandbox.io/p/sandbox/naughty-pine-j2zw59 |
Beta Was this translation helpful? Give feedback.
Hi, I am assuming you have created a new Vue3 project, which uses Vite.
In which case, you are able to make use of the Asset API to load your binary file.
The previous answers are right in saying there's not a quick and easy way to access the server file system from a Vue component/web page, since the Vue3 Vite template makes use of purely client side rendering (example: your site is built into static HTML/CSS/JS files). Therefore, the most optimal solution would be to instruct the bundler to include your binary file, using the Asset API mentioned above.
Hope this helps, I'll attach a demo when I get to my computer. 😃
Update: demo link https://codesandbox.io/p/sandbox/naughty-pine-j2zw59