Access Files on MATLAB Web App Server
MATLAB® Web App Server™ allows you to deploy and share MATLAB web apps through a web browser interface. When working with these web apps, users often need to access, manipulate, and exchange files between the server and client machines. You can package files along with a web app, read and write to files on the server, and download files to a local client.
For information on setting up a MATLAB Web App Server instance, see Set Up MATLAB Web App Server.
Package Files in Web App
Files included in the web app archive at compile time are embedded in the CTF and added
to the path when the web app runs. Files are located at the deployable archive root
ctfroot in a cache folder on the server. These files should be
treated as read-only.
To specify paths to packaged files in your web app code, locate the file using the
which function or specify the file location relative to
ctfroot. Use isdeployed to toggle between local
and deployed paths.
For more information on including files at compile time, see Include and Access Files in Packaged Applications (MATLAB Compiler).
Include MEX Files, DLLs, or Shared Libraries
When you compile MATLAB functions containing MEX files, ensure that the dependency analysis process can find them. In particular, note that:
Since the dependency analysis function cannot examine MEX files, DLLs, or shared libraries to determine their dependencies, explicitly include all executable files these files require.
If you have any doubts that a MATLAB function called by a MEX file, DLL, or shared library can be found during dependency analysis, then manually include that function.
Not all functions are compatible with the compiler. Check the file
mccExcludedFiles.logafter your build completes. This file lists all functions called from your application that you cannot deploy.
Write to File During Web App Session
During a running web app session, files read from or written to the current working
directory write to the session folder, which is a local folder on the server that is
created for each running session of a web app and given a unique ID for each session. The
session folder is automatically added to the path for a running app session and acts as the
current working directory. Folders are not shared between sessions, files in a session
folder are not available in other app sessions, and each folder is automatically deleted
when the session ends. You can use the session folder in workflows involving
uigetfile and uiputfile to pass data between the
server and client machines.
Server File Storage Locations
You can read/write to a specific file location on the server by using a hardcoded path in your MATLAB code. The path must point to a location that exists in the server storage, and paths must be specific to the file system on the server machine. Changes made to files in static locations on the server are shared across all app sessions.
In order for your web apps to interact with local files or directories that are not
along the app search path (i.e. outside of the app/session caches), the worker account
that runs the apps service must have the necessary permissions to read and write to
these locations. By default, worker accounts do not have read/write permissions to most
locations on the server machine. You must explicitly grant the worker account read/write
permissions to these files or folders on the server machine using a command-line tool
such as chmod or by editing permissions in Windows® Explorer. For cloud deployments, you must remotely connect to the server
instance to set file permissions. For more information on the service accounts, see
Set Up MATLAB Web App Server.
Shared or Network Locations
You can read and write to a database or network location from a web app. The database or network location must be accessible from MATLAB Web App Server. Changes made to shared locations persist across all app sessions. The MATLAB Web App Server secrets management capabilities can be useful in ensuring security with this type of workflow. For details, see Handle Sensitive Information in Deployed Applications.
The worker account must have read and write access to the shared location. For network locations, this often requires that the worker uses a network or domain account, as you typically cannot grant local accounts access to non-local file locations.
Note
Some additional setup steps, toolboxes, or drivers may be necessary to deploy web apps that access databases. For example, to deploy a web app that uses the MySQL native interface, you must first install the MySQL/C++ Connector file on the server machine, as shown in Deploy MySQL Native Interface Database Application with MATLAB Compiler (Database Toolbox).
Download Files From Web App
To save files to a client machine from a web app, use uiputfile to
get the path to the download location. You can use this path as the destination in write
functions such as save and exportgraphics to download
files to the client machine.
For example, save the graph displayed on the app component app.UIAxes
as a PNG
file.
% Get the path to the download location [f, p] = uiputfile('*.png'); % Save the plot to the specified path exportgraphics(app.UIAxes,fullfile(p,f));
Files downloaded from a web app are always placed in the download location specified by
the client browser. The user can specify a file name in the uiputfile
dialogue box, but the destination folder can only be changed from their browser settings.
While uiputfile and uigetfile are supported for
passing files to/from web apps, the uigetdir function is not supported
for use with web apps. For more information, see Web App Limitations and Unsupported Functionality.
See Also
ctfroot (MATLAB Compiler) | isdeployed (MATLAB Compiler) | uigetfile (MATLAB) | uiputfile (MATLAB) | save (MATLAB) | which (MATLAB)