Bahar önyükleme ve Angular JS kullanıyorum. Dosya indirmek için kullandığım bir Spring REST Controller var. http://localhost:8080/download'u kullanarak aradığımda çalışır ve dosya indirilir. Şimdi bir butonum var, üzerine tıkladığımda, dosya indirilecek. Bu yüzden bahar web servisimin URL'sini almak için açısal js denetleyicimde bir işlev yazdım, ancak test ettiğimde hiçbir şey olmadı. Bunu düzeltmek için ne yapmalıyım? İlkbahar ve Açısal kullanarak dosyaları indirmek için daha iyi bir yolu var mı?Angular JS ve bir Spring tabanlı RESTful web hizmeti kullanarak bir dosya indirmek
/**
* Size of a byte buffer to read/write file
*/
private static final int BUFFER_SIZE = 4096;
private String filePath = "C:\\Users\\marwa\\Desktop\\marwa\\gg.jpg";
/**
* Method for handling file download request from client
*/
@RequestMapping (value="/download", method=RequestMethod.GET)
public void doDownload(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// get absolute path of the application
ServletContext context = request.getServletContext();
String appPath = context.getRealPath("");
System.out.println("filepath = " + filePath);
// construct the complete absolute path of the file
File downloadFile = new File(filePath);
FileInputStream inputStream = new FileInputStream(downloadFile);
// get MIME type of the file
String mimeType = context.getMimeType(filePath);
if (mimeType == null) {
// set to binary type if MIME mapping not found
mimeType = "application/octet-stream";
}
System.out.println("MIME type: " + mimeType);
// set content attributes for the response
response.setContentType(mimeType);
response.setContentLength((int) downloadFile.length());
// set headers for the response
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",
downloadFile.getName());
response.setHeader(headerKey, headerValue);
// get output stream of the response
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;
// write bytes read from the input stream into the output stream
while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outStream.close();
}
benim açısal js fonksiyonu (Ben bahar denetleyicisinden bir sonuç elde olmadığını görmek için console.log ("Tamam") eklenir ve Tamam baskılı)
$scope.downloadFile = function() {
$http({method: 'GET', url: '/download'}).
success(function(result) {
console.log("ok");
}).
error(function(data, status, headers, config) {
console.log("oops");
});
};
ve benim butonuna
<button class="btn btn-success" ng-click="downloadFile()">download</button>
Yardımcı olup olmadığından emin değilim, ancak şunu kontrol etmek isteyebilirsiniz: http://stackoverflow.com/questions/17177303/spring-download-response-as-a-file –