MATLAB Answers

Morteza
0

How can I send a file via Web Application

Asked by Morteza
on 21 Oct 2018
Latest activity Commented on by Hunter Casillas on 17 Oct 2019 at 17:31
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.

1 Answer

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

  3 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.

Sign in to comment.