I’m working on a React / VTK.js project,
In VTK.js to read a VTK file we call this function with the directory of the VTK file (that must be in the public folder tree structure).
const reader1 = vtkPolyDataReader.newInstance();
reader1
.setUrl(`Ressources/MrSKIN.vtk`)
.then(async () => {
pd = await reader1.getOutputData(0);
...
I need to read a VTK file in other "system directory":
reader1
.setUrl(`C:/Users/admin/data/MrSkin.vtk`)
But that doesn’t work, I’ve read that I must re-configure the webpack file but I don’t know how to exactly do it
2
Answers
First thing, you can’t directly access files outside your project directory in React (security impositions by web browsers). Therefore reading the VTK file won’t work.
If you want to make it work here’s a step by step on how to do it:
1.) Firstly setup a server with Express Js
2.) Create a static file to host your files
3.) Upload files on the said static server after configuring it for serving purpose (Use Express’s ‘express.static’ for this)
Here’s an example:
In this example, files in the uploads directory will be accessible from http://yourserver.com/uploads.
Now last step is to modify your React code to read the VTK file using its URL on the server:
replace /uploads/MrSkin.vtk with the actual URL path to your uploaded VTK file on the server.
To read a VTK file in a different system directory in a React/VTK.js project, you need to reconfigure the webpack file. Here are the general steps to follow:
Eject from the React app: You need to eject from the React app to have full control over the webpack configuration. To eject, run the following command in the terminal:
Configure the webpack file: After ejecting, you will have a config folder in your project directory. Inside this folder, you will find a webpack.config.js file. Open this file and add the following code to the module.exports object:
Replace ../path/to/vtk.js with the path to the vtk.js file on your system.
Import vtk.js: In your React component, you can now import vtk.js using the following code:
You can then use the vtkPolyDataReader to read the VTK file from the specified directory.
It’s worth noting that ejecting from a React app can be a complex process, and it’s important to understand the implications of doing so before proceeding. Additionally, modifying the webpack configuration can have unintended consequences, so it’s important to test your changes thoroughly before deploying your application.