csh_template_download.lsc 1.91 KB
Newer Older
Spencer Chang's avatar
Spencer Chang committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<?xml version="1.0" encoding="UTF-8"?>
<a:service xmlns:s="leaf.plugin.script" xmlns:a="http://www.leaf-framework.org/application" trace="true">
    <a:init-procedure>
        <s:server-script><![CDATA[
            importPackage(java.io);
            
            function transfer(file, os) {
                var fis = new FileInputStream(file);
                var b = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024 * 64);
                var len = -1;
                while ((len = fis.read(b)) != -1) {
                    os.write(b, 0, len);
                }
                fis.close();
            }
            
            
            function getdate() {
                var now = new Date()
                y = now.getFullYear()
                m = now.getMonth() + 1
                d = now.getDate()
                m = m < 10 ? "0" + m : m
                d = d < 10 ? "0" + d : d
                return y + "" + m + "" + d
            }
            

            $ctx["__request_type__"] = 'file'; //to indicate this request is not a JSON_REQUEST
            var resp = $ctx['_instance.javax.servlet.http.HttpServletResponse'];
            resp.setHeader("Pragma", "No-cache");
            resp.setHeader("Cache-Control", "no-cache, must-revalidate");
            //println(decodeURI($ctx.parameter.doc_code));
            var path = $ctx.parameter.path;
            resp.setDateHeader("Expires", 0);
            resp.setContentType("application/x-msdownload");
            var file_name = $ctx.parameter.file_name;
            var file_path = $ctx.parameter.file_path;
            if(file_name){
                resp.setHeader("Content-disposition", "attachment; filename=" + encodeURI(file_name, 'utf-8'));
                var os = resp.getOutputStream();
                transfer(file_path + file_name, os);
                os.flush();
            }
        ]]></s:server-script>
    </a:init-procedure>
</a:service>