How can I send a file via Web Application

Hi I deployed a web application. After analyzing data imported by user through web browser, I want to send results as an excel file that can be downloaded by user. How should I do that?

 Accepted Answer

Users can upload their files by uigetfile and download result by uiputfile. Please keep in mind that uigetfile and uiputfile are supported in WebApps from R2018b.
The following examples can import users' uploaded CSV file and download Excel file. The downloaded file will be found browser's download folder (for example, if Windows, C:\Users\USERNAME\Downloads)
uigetfile example:
% Button pushed function: UploadButton
function UploadButtonPushed(app, event)
[file, path] = uigetfile({'*.csv*'}, 'File Selector', 'MultiSelect', 'on');
if isequal(file,0)
disp('Canceled')
else
% Read input file
inpuFilePath = fullfile(path,file);
tbl = readtable(inpuFilePath);
% Do some calculation
app.resultTbl = table;
app.resultTbl.col1 = tbl.col1 + tbl.col2 + tbl.col3;
end
end
uiputfile example:
% Button pushed function: DownloadButton
function DownloadButtonPushed(app, event)
[file,path] = uiputfile('results.xlsx');
resultFilePath = fullfile(path,file);
writetable(app.resultTbl, resultFilePath)
end
Also, I've attached sample mlapp file and csv file for this demo. Please see Sample_mlapp.zip. If compiled as a WebApps, the app would look like this.
Users can download results.xlsx into their machine.

13 Comments

Thanks for your complete and clear answer. But I encountered a problem, I cannot download the attached file. Is it possible to send it as email to morteza.mohajeri@gmail.com ?
Thanks again, I compiled the attached file and it works properly and my problem is solved.
Is there any way to do this with a PDF file? Writetable is only compatible with .xlsx files.
Hi Kojiro, Thanks for the files above. I have used the above logic to try and download a file to local PC. When compiled as a webapp and running on a server on my local machine, then the file is downloaded, but the location is a seemingly random (I appreciate there is logic, however without that knowlegde, it appears random) session folder located in the mdwas Appdata path. It also seems to change every time there is a new session, so tracking down the files is tricky. The only way I know of to determine the path is to disp the location on the webapp log, however security wise this is not great.
My real issue is the deployed web app which runs on an Azure server. If I download the file using the above logic, without being able to specify the path to my local machine, it saves it in the same seemngly random path as described above on the server.
The users who use the webapp would like to download the output of some calculations to their PC.
Am I missing something, is this possible?
@David Mitchell,
When writetable or other file writer functions are called, Web Apps will create a file in the sesstion folder (a random file path) inside mdwas sessions folder, for example of Windows, C:\Users\MwWebAppsGuestR2019b\AppData\Local\MathWorks\webapps\9.7\services\mdwas\sessions\5f59ca03-f4ea-4839-a192-6c251bd769ef.
After the file is created, web browser will download the file from the session folder from Web Apps Server and locate it to Download folder (for example, C:\Users\XXX\Downloads or any foloder you pointed in web browser setting), so, I think you can specify the path to your local machine by specifying a download folder in web browser.
Kojiro, Thanks for the info.
I checked my downloads folder and could not see the files. So I checked my broswer (Chrome) was pointed at my C:\Users\####\Downloads which it was. I then toggled on the "ask where to save each file before downloading" option and still no luck. There was no dialog to ask me where to download to, so it does not seem to be triggering a download on the broswer after successfully saving to the local sessions folder.
FYI, I am using Chrome Version 78.0.3904.108 (Official Build) (64-bit) and 2019b webapp server and matlab (latest versions).
@David Mitchell
Could you confirm that issue occurs in other web browsers (such as Edge, Firefox)?
Same on firefox and edge, no file in my downloads folder
So, an error might occur. Could you check the app log by clicking "show log" button at the left bottom of the web app?
The only extra line from pressing the download button was (no errors):
Starting download for <filename>.dat
As of Windows, File Explorer sometimes takes long time (for example, a minute) to show after invoking upload / download in Web browsers. Could you stay more minutes after pressing the download button and check the download actually occurs?
it seems like the uigetdir cannot be supported in web apps. You should show a notification if we use this function when the web app is complied.
Has this been resolved? I believe I am experiencing the exact same issue.
I'm not reading; only writing to an Excel (or .csv, if I have to) file. It works perfectly as an app on my computer, but when deployed, I get the "Starting download for test1.xls" message in log, but never receive a file in my Downloads directory.
I'm using writematrix, rather than writetable, but otherwise, my script looks much like the uiputfile example, above.

Sign in to comment.

More Answers (1)

Hello,
This theath has show quite well how to download an excel table using the command writetable. My problem is that I have generated a pdf report with reportgenerator and I want use saveas instead of writetable. But saveas is not supported in webapps.
How can I send the pdf report to the user?.
Thanks.

3 Comments

I was able to send a pdf to the user using the following code:
Where report.OutputPath is the path to where the pdf is stored on the server.
web(report.OutputPath);
You basically "saved my life"!!!!! Great!
@Hunter Casillas's solution works great for non-Excel files. Thanks!

Sign in to comment.

Categories

Find more on MATLAB Web App Server in Help Center and File Exchange

Products

Asked:

on 21 Oct 2018

Commented:

on 30 Jan 2024

Community Treasure Hunt

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

Start Hunting!