0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Angular ASP.NET FileSaver.jsを使ってファイルのダウンロードを実装する

Last updated at Posted at 2016-03-02

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);
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?