模仿ajax利用rest导出文件

前端之家收集整理的这篇文章主要介绍了模仿ajax利用rest导出文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
var form=$("<form>");//定义一个form表单
	form.attr("style","display:none");
	form.attr("target","");
	form.attr("method","get");
	form.attr("action",serverUrl);
	var input1=$("<input>");
	input1.attr("type","hidden");
	input1.attr("name","dicGuid");
	input1.attr("value",dictionaryGuid);
	$("body").append(form);//将表单放置在web中
	form.append(input1);

	form.submit();//表单提交 

public Representation get() throws ResourceException {
		Form form = getRequest().getResourceRef().getQueryAsForm() ;    //获取查询参数  
		String dicGuid = form.getFirstValue(PARAM_DICTIONARY_DICTIONARYID).trim();
		DictionaryBean dicBean;
		try {
			dicBean = ChapterUtil.getDictionary(dicGuid);
		} catch (FileException e) {
			throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,"获取词典库失败");
		}
		String name = dicBean.getName();
		String content = listToStr(dicBean.getContent());
		
		final byte[] bpmnBytes;
		try {
			bpmnBytes = content.getBytes("UTF-8");
			Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
		    disposition.setFilename(new String(name.getBytes("utf-8"),"ISO_8859_1") + ".txt");
			OutputRepresentation output = new OutputRepresentation(MediaType.APPLICATION_OCTET_STREAM) {
	             
				public void write(OutputStream os)
	                     throws IOException
	             {
	                 os.write(bpmnBytes);
	                 os.flush();
	             }
	         };
	         output.setDisposition(disposition);
	         return output;
		} catch (UnsupportedEncodingException e) {
			throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,"获取词典库失败");
		}
	}
原文链接:https://www.f2er.com/ajax/162898.html

猜你在找的Ajax相关文章