当我单击下载按钮时,我想显示“另存为”窗口。
但是,当单击下载按钮时,“另存为”不会显示并立即下载。
我将response.setContentType的应用程序/八位字节流更改为x-download和zip,但是“另存为”未打开。
我该怎么办?
html
<button type="button" class="btn btn-large btn-danger" ng-click="serverInfoDownload()">Download</button>
controller.js
$scope.serverInfoDownload = function(){
fileDownload.downloadFile(function (success){
console.log("download success");
}, function(error){
console.log("error", error);
});
$(".serverInfoDownload").modal('hide');
}
service.js
angular.module('oggApp')
.factory('fileDownload', ['$resource', function ($resource) {
return $resource('/api/download/',{
}, {
downloadFile: {
url: '/api/download/download',
method: 'GET'
}
}
);
}]);
controller.java
@RequestMapping(value="download", method=RequestMethod.GET)
public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
backup.backupDatabase();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
String fileName = backup.getFileName();
File downloadfile = new File(backup.getUrl() + fileName+".zip");
if (downloadfile.exists() && downloadfile.isFile()) {
response.setContentType("application/octet-stream; charset=utf-8");
response.setContentLength((int) downloadfile.length());
ServletOutputStream os = null;
FileInputStream fis = null;
try {
response.setHeader("Content-Disposition", getDisposition(fileName+".zip", check_browser(request)));
response.setHeader("Content-Transfer-Encoding", "binary");
os = response.getOutputStream();
fis = new FileInputStream(downloadfile);
FileCopyUtils.copy(fis, os);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null){
fis.close();
}
os.flush();
os.close();
} catch (Exception e2) {}
}
}else {
System.out.println("FILE Not Found");
}
}
private String getDisposition(String down_filename, String browser_check) throws UnsupportedEncodingException {
String prefix = "attachment; filename=";
String encodedfilename = null;
System.out.println("browser_check:"+browser_check);
if (browser_check.equals("ie")) {
encodedfilename = URLEncoder.encode(down_filename, "UTF-8").replaceAll("\\+", "%20");
}else if (browser_check.equals("chrome")) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < down_filename.length(); i++){
char c = down_filename.charAt(i);
if (c > '~') {
sb.append(URLEncoder.encode("" + c, "UTF-8"));
} else {
sb.append(c);
}
}
encodedfilename = sb.toString();
}else {
encodedfilename = "\"" + new String(down_filename.getBytes("UTF-8"), "8859_1") + "\"";
}
return prefix + encodedfilename;
}