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?

 Akzeptierte Antwort

Kojiro Saito
Kojiro Saito am 21 Okt. 2018

5 Stimmen

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 Kommentare

Morteza
Morteza am 21 Okt. 2018
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 ?
Morteza
Morteza am 21 Okt. 2018
Thanks again, I compiled the attached file and it works properly and my problem is solved.
Hunter Casillas
Hunter Casillas am 17 Okt. 2019
Is there any way to do this with a PDF file? Writetable is only compatible with .xlsx files.
David Mitchell
David Mitchell am 22 Nov. 2019
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?
Kojiro Saito
Kojiro Saito am 22 Nov. 2019
@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.
David Mitchell
David Mitchell am 25 Nov. 2019
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).
Kojiro Saito
Kojiro Saito am 26 Nov. 2019
Bearbeitet: Kojiro Saito am 26 Nov. 2019
@David Mitchell
Could you confirm that issue occurs in other web browsers (such as Edge, Firefox)?
David Mitchell
David Mitchell am 27 Nov. 2019
Same on firefox and edge, no file in my downloads folder
Kojiro Saito
Kojiro Saito am 28 Nov. 2019
So, an error might occur. Could you check the app log by clicking "show log" button at the left bottom of the web app?
David Mitchell
David Mitchell am 3 Dez. 2019
The only extra line from pressing the download button was (no errors):
Starting download for <filename>.dat
Kojiro Saito
Kojiro Saito am 4 Dez. 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 am 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 am 23 Jan. 2021
Bearbeitet: Cathie Kessler am 25 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Roberto M Sanchez
Roberto M Sanchez am 3 Dez. 2019

1 Stimme

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 Kommentare

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);
Alessandro
Alessandro am 27 Feb. 2023
You basically "saved my life"!!!!! Great!
Osborn
Osborn am 30 Jan. 2024
@Hunter Casillas's solution works great for non-Excel files. Thanks!

Melden Sie sich an, um zu kommentieren.

Produkte

Gefragt:

am 21 Okt. 2018

Kommentiert:

am 30 Jan. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by