download_single_file.js 1.78 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 48 49 50 51 52 53 54 55 56 57 58
	importPackage(java.io);
	            
	function write_os_to_file(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 update_file_size(file_path,file_name,tmp_id){
		var file = new File(file_path + file_name);
		$bm('prj.PRJ501N.bgfl_generate_word_tmp').update({
			file_size	   :file.length(),
            tmp_id         :tmp_id
		});
		
	}
	
	function download_file(file_path,file_name){
		$ctx["__request_type__"] = 'file'; // request file not JSON
	    var resp = $ctx['_instance.javax.servlet.http.HttpServletResponse'];
	    resp.setHeader("Pragma", "No-cache");
	    resp.setHeader("Cache-Control", "no-cache, must-revalidate");
	    resp.setHeader("Content-disposition", "attachment; filename=" + encodeURI(file_name,'utf-8'));
	    resp.setDateHeader("Expires", 0);
	    resp.setContentType("application/x-msdownload");
	    try{
	    	var os = resp.getOutputStream();
	    	write_os_to_file(file_path+file_name, os);
		    os.flush();
	    }catch(e){
	    	$logger("server-script").severe(e.message);
	    }
	}
	
	function download(tmp_id){
		var tmp = $bm('prj.PRJ501N.bgfl_generate_word_tmp');
        var result = tmp.queryAsMap({
        	tmp_id:tmp_id
        });
        var arr = result.getChildren();
        download_file(arr[0].file_path,arr[0].file_name);
	}
	
	function upload_atm(tmp_id,document_category,document_table,document_id,tab_group){
		$bm('prj.PRJ501N.bgfl_generate_word_tmp').execute({
			document_category:document_category,
			document_table :document_table,
            document_id    :document_id,
            tmp_id         :tmp_id,
            tab_group	   :tab_group
		});
	}