Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mp_streamfile.sas
Go to the documentation of this file.
1 /**
2  @file mp_streamfile.sas
3  @brief Streams a file to _webout according to content type
4  @details Will set headers using appropriate functions (SAS 9 vs Viya) and send
5  content as a binary stream.
6 
7  Usage:
8 
9  filename mc url "https://raw.githubusercontent.com/sasjs/core/main/all.sas";
10  %inc mc;
11 
12  %mp_streamfile(contenttype=csv,inloc=/some/where.txt,outname=myfile.txt)
13 
14  <h4> Dependencies </h4>
15  @li mf_getplatform.sas
16  @li mp_binarycopy.sas
17 
18  @param contenttype= Either TEXT, ZIP, CSV, EXCEL (default TEXT)
19  @param inloc= /path/to/file.ext to be sent
20  @param outname= the name of the file, as downloaded by the browser
21 
22  @author Allan Bowe
23  @source https://github.com/sasjs/core
24 
25 **/
26 
27 %macro mp_streamfile(
28  contenttype=TEXT
29  ,inloc=
30  ,outname=
31 )/*/STORE SOURCE*/;
32 
33 %let contentype=%upcase(&contenttype);
34 %local platform; %let platform=%mf_getplatform();
35 
36 %if &contentype=ZIP %then %do;
37  %if &platform=SASMETA %then %do;
38  data _null_;
39  rc=stpsrv_header('Content-type','application/zip');
40  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
41  run;
42  %end;
43  %else %if &platform=SASVIYA %then %do;
44  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.zip'
45  contenttype='application/zip'
46  contentdisp="attachment; filename=&outname";
47  %end;
48 %end;
49 %else %if &contentype=EXCEL %then %do;
50  /* suitable for XLS format */
51  %if &platform=SASMETA %then %do;
52  data _null_;
53  rc=stpsrv_header('Content-type','application/vnd.ms-excel');
54  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
55  run;
56  %end;
57  %else %if &platform=SASVIYA %then %do;
58  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls'
59  contenttype='application/vnd.ms-excel'
60  contentdisp="attachment; filename=&outname";
61  %end;
62 %end;
63 %else %if &contentype=XLSX %then %do;
64  %if &platform=SASMETA %then %do;
65  data _null_;
66  rc=stpsrv_header('Content-type','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
67  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
68  run;
69  %end;
70  %else %if &platform=SASVIYA %then %do;
71  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls'
72  contenttype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
73  contentdisp="attachment; filename=&outname";
74  %end;
75 %end;
76 %else %if &contentype=TEXT %then %do;
77  %if &platform=SASMETA %then %do;
78  data _null_;
79  rc=stpsrv_header('Content-type','application/text');
80  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
81  run;
82  %end;
83  %else %if &platform=SASVIYA %then %do;
84  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt'
85  contenttype='application/text'
86  contentdisp="attachment; filename=&outname";
87  %end;
88 %end;
89 %else %if &contentype=CSV %then %do;
90  %if &platform=SASMETA %then %do;
91  data _null_;
92  rc=stpsrv_header('Content-type','application/csv');
93  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
94  run;
95  %end;
96  %else %if &platform=SASVIYA %then %do;
97  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt'
98  contenttype='application/csv'
99  contentdisp="attachment; filename=&outname";
100  %end;
101 %end;
102 %else %if &contentype=HTML %then %do;
103  %if &platform=SASVIYA %then %do;
104  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name="_webout.json"
105  contenttype="text/html";
106  %end;
107 %end;
108 %else %do;
109  %put %str(ERR)OR: Content Type &contenttype NOT SUPPORTED by &sysmacroname!;
110  %return;
111 %end;
112 
113 %mp_binarycopy(inloc="&inloc",outref=_webout)
114 
115 %mend;