HTML側
<script src="~/Scripts/Blob.js"></script>
<script src="~/Scripts/FileSaver.min.js"></script>
<button type="submit" class="btn btn-default" ng-click="DownloadExcel()">
<img src="~/Images/blue-document-excel.png" />{{filename}}
</button>
Angular 側
//ファイルネームを指定してダウンロードする
$scope.DownloadExcel = function () {
url = '/FileDownload/Download/';
$http({
url: url,
responseType: "blob",
params: { fileName: $scope.filename },
})
.then(function (response) {
var blob = new Blob([response.data], { type: "application/octet-stream" });
saveAs(blob, $scope.filename);
}, function (response) {
alert("error downloading file from " + downloadUrl);
});
}
$http.getやpostを使うとファイルダウンロードがうまくいかなかったので、
$http()を使っている。
ASP.NET側
public class FileDownloadController : Controller
{
public FileResult Download(string fileName)
{
var filepath = @"c:\temp";
string fileFullPath = System.IO.Path.Combine(excelRepositoryPath, fileName);
return File(fileFullPath, "application/excel", fileName);
}
}