将zip文件从REST返回到客户端不会弹出文件

时间:2016-03-19 10:15:14

标签: ajax rest download jersey zipfile

我正在尝试通过添加library(ggplot2) set.seed(123) nobs <- 1000 dat <- data.frame(var1=sample(LETTERS[1:3],nobs, T), var2=sample(LETTERS[1:2],nobs,T), var3=sample(LETTERS[1:3],nobs,T), y=rnorm(nobs)) p1 <- ggplot(dat, aes(x=var1, y=y)) + geom_boxplot(aes(fill=var2), position=position_dodge(.9)) + facet_wrap(~var3) + stat_summary(fun.y=mean, geom="point", aes(group=var2), position=position_dodge(.9), color="black", size=4) Content-Type执行以下操作来下载从REST发送的zip文件:

服务器侧

Content-Disposition

客户端:

另一方面,客户端期望@Produces("application/x-zip-compressed") public Response export(@PathParam("username") String username, @Context UriInfo ui) { long timeStamp = new Date().getTime(); String exportedFolder = "/home/madmin/pods/"+username+"/download/"; String Path = ui.getRequestUri().toString() .replace(replaceMe + username, ""); Path = Path.replace("http://", "https://"); Path = Path.replace(",system/export", ""); String outputZipFile = exportedFolder+"exported_on_"+timeStamp+".zip"; String sourceFolder = null; File file = new File(exportedFolder); if (!file.exists()) { if (file.mkdirs()) { System.out.println("Directory is created!"); } else { System.out.println("Failed to create directory for exporting!"); //return ; } } constructGraphs cGraphs = new constructGraphs(Path, username); sourceFolder = cGraphs.writeGraphFiles(); generateZipFile appZip = new generateZipFile(sourceFolder,outputZipFile); appZip.generateFileList(new File(sourceFolder)); appZip.zipIt(outputZipFile); //Read the outputZipFile as inputStream and return it FileInputStream fileIs = null; try { fileIs = new FileInputStream(outputZipFile); } catch (IOException e) { throw new WebApplicationException(404); } String fileName= outputZipFile.substring(outputZipFile.lastIndexOf("/")+1, outputZipFile.length()); return Response.status(Status.OK).entity(fileIs).header("Content-Disposition","attachment; filename = "+fileName).build(); } 如下:

application/x-zip-compressed

文件没有弹出,而是在响应中看到输入流。

我在这里做错了吗?任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

使用AJAX是不可能的,我假设你在这种情况下使用angularjs http serivce。请参阅:Why threre is no way to download file using ajax request?

您有不同的选择来解决您的问题。 例如:easiest way to open a download window without navigating away from the pageHow do you serve a file for download with AngularJS or Javascript?