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
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> SAS Macros </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 inref= fileref of file to be sent (if provided, overrides `inloc`)
21  @param outname= the name of the file, as downloaded by the browser
22 
23  @author Allan Bowe
24  @source https://github.com/sasjs/core
25 
26 **/
27 
28 %macro mp_streamfile(
29  contenttype=TEXT
30  ,inloc=
31  ,inref=0
32  ,outname=
33 )/*/STORE SOURCE*/;
34 
35 %let contentype=%upcase(&contenttype);
36 %local platform; %let platform=%mf_getplatform();
37 
38 %if &contentype=ZIP %then %do;
39  %if &platform=SASMETA %then %do;
40  data _null_;
41  rc=stpsrv_header('Content-type','application/zip');
42  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
43  run;
44  %end;
45  %else %if &platform=SASVIYA %then %do;
46  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.zip'
47  contenttype='application/zip'
48  contentdisp="attachment; filename=&outname";
49  %end;
50 %end;
51 %else %if &contentype=EXCEL %then %do;
52  /* suitable for XLS format */
53  %if &platform=SASMETA %then %do;
54  data _null_;
55  rc=stpsrv_header('Content-type','application/vnd.ms-excel');
56  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
57  run;
58  %end;
59  %else %if &platform=SASVIYA %then %do;
60  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls'
61  contenttype='application/vnd.ms-excel'
62  contentdisp="attachment; filename=&outname";
63  %end;
64 %end;
65 %else %if &contentype=XLSX %then %do;
66  %if &platform=SASMETA %then %do;
67  data _null_;
68  rc=stpsrv_header('Content-type','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
69  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
70  run;
71  %end;
72  %else %if &platform=SASVIYA %then %do;
73  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.xls'
74  contenttype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
75  contentdisp="attachment; filename=&outname";
76  %end;
77 %end;
78 %else %if &contentype=TEXT %then %do;
79  %if &platform=SASMETA %then %do;
80  data _null_;
81  rc=stpsrv_header('Content-type','application/text');
82  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
83  run;
84  %end;
85  %else %if &platform=SASVIYA %then %do;
86  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt'
87  contenttype='application/text'
88  contentdisp="attachment; filename=&outname";
89  %end;
90 %end;
91 %else %if &contentype=CSV %then %do;
92  %if &platform=SASMETA %then %do;
93  data _null_;
94  rc=stpsrv_header('Content-type','application/csv');
95  rc=stpsrv_header('Content-disposition',"attachment; filename=&outname");
96  run;
97  %end;
98  %else %if &platform=SASVIYA %then %do;
99  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name='_webout.txt'
100  contenttype='application/csv'
101  contentdisp="attachment; filename=&outname";
102  %end;
103 %end;
104 %else %if &contentype=HTML %then %do;
105  %if &platform=SASVIYA %then %do;
106  filename _webout filesrvc parenturi="&SYS_JES_JOB_URI" name="_webout.json"
107  contenttype="text/html";
108  %end;
109 %end;
110 %else %do;
111  %put %str(ERR)OR: Content Type &contenttype NOT SUPPORTED by &sysmacroname!;
112  %return;
113 %end;
114 
115 %if &inref ne 0 %then %do;
116  %mp_binarycopy(inref=&inref,outref=_webout)
117 %end;
118 %else %do;
119  %mp_binarycopy(inloc="&inloc",outref=_webout)
120 %end;
121 
122 %mend;