How to fetch files inside uihtml in MATLAB web app

4 次查看(过去 30 天)
I'm using the uihtml component inside uifigure to load a html file. This html file want to fetch a .MOD file or .txt file from local storage, location is static/data/xxx.txt.
just like this
<script>
fetch("./static/data/example.txt")
.then(response => response.text)
.then(textString => {
document.getElementById('viewer').innerHTML = textString;
});
</script>
The problem is I got a 404 error, the file not found. Seems there are some ways to fetch the data but I didn't know, because the .js code can be found in the same location static/js/xxx.js. Are there any way to fetch this data?

回答(1 个)

Vidip Jain
Vidip Jain 2023-8-31
I understand that you are using the “uihtml component inside “uifigure to load a html file and the relative paths inside the HTML file, this might not work as expected because the HTML is embedded within a MATLAB application and is not being served directly from a web server. This can cause issues with relative paths when fetching files.
As a work around for this, you can use MATLAB's “web” function to load your HTML file and specify a local server to serve the files needed by your HTML content.
You can place your files within your MATLAB project directory and use the web function to set up a local server that serves your files. This local server can handle relative paths in your HTML file, then update the URLs for fetching files using an absolute path relative to the local server.
Refer to this documentation for more information: https://www.mathworks.com/help/matlab/ref/web.html

类别

Help CenterFile Exchange 中查找有关 MATLAB Web App Server 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by