jquery 下载图片另存为

1,场景:附件列表上传操作字段,有一个下载按钮,现在附件,当附件类型为图片时,A标签无法另存为。
2,解决办法,采用表单提交的方式,由后端java处理完,采用iamgeIo输出到页面。
前端代码



js
function downloadImg(imagePathURL){
$("#queryForm").attr("action","${request.getContextPath()}/ajaxUpload/downloadPic");
$("#queryForm").submit();
}

java代码
def downloadPic(){
String path = params.path;
String[] spUrl = path.toString().split("/");
int len = spUrl.length;
String endUrl = spUrl[len - 1];
int dot = endUrl.lastIndexOf('.');
String suffix = endUrl.substring(dot + 1, endUrl.length());

URL url = new URL(path);
BufferedImage img = ImageIO.read(url);

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(endUrl, "UTF-8"))));

OutputStream os = response.getOutputStream();

ImageIO.write(img,suffix,os);

os.close();
}


3,效果
[img]http://dl2.iteye.com/upload/attachment/0099/4279/e32123ef-bf55-36de-80e3-c22b82ca7952.jpg[/img]

你可能感兴趣的:(代码)