模仿ajax利用rest导出文件

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

  1. public Representation get() throws ResourceException {
  2. Form form = getRequest().getResourceRef().getQueryAsForm() ; //获取查询参数
  3. String dicGuid = form.getFirstValue(PARAM_DICTIONARY_DICTIONARYID).trim();
  4. DictionaryBean dicBean;
  5. try {
  6. dicBean = ChapterUtil.getDictionary(dicGuid);
  7. } catch (FileException e) {
  8. throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,"获取词典库失败");
  9. }
  10. String name = dicBean.getName();
  11. String content = listToStr(dicBean.getContent());
  12. final byte[] bpmnBytes;
  13. try {
  14. bpmnBytes = content.getBytes("UTF-8");
  15. Disposition disposition = new Disposition(Disposition.TYPE_ATTACHMENT);
  16. disposition.setFilename(new String(name.getBytes("utf-8"),"ISO_8859_1") + ".txt");
  17. OutputRepresentation output = new OutputRepresentation(MediaType.APPLICATION_OCTET_STREAM) {
  18. public void write(OutputStream os)
  19. throws IOException
  20. {
  21. os.write(bpmnBytes);
  22. os.flush();
  23. }
  24. };
  25. output.setDisposition(disposition);
  26. return output;
  27. } catch (UnsupportedEncodingException e) {
  28. throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,"获取词典库失败");
  29. }
  30. }

猜你在找的Ajax相关文章