MATLAB Answers

How can I send a file via Web Application

74 views (last 30 days)
Morteza
Morteza on 21 Oct 2018
Edited: Cathie Kessler on 25 Jan 2021
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?

  0 Comments

Sign in to comment.

Accepted Answer

Kojiro Saito
Kojiro Saito on 21 Oct 2018
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

Kojiro Saito
Kojiro Saito on 4 Dec 2019
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?
Yuhao Sun
Yuhao Sun on 26 Aug 2020
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.
Cathie Kessler
Cathie Kessler on 23 Jan 2021
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)

Roberto M Sanchez
Roberto M Sanchez on 3 Dec 2019
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.

  1 Comment

Hunter Casillas
Hunter Casillas on 3 Jan 2020
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);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by