0

My AngularJS application is interacting with ASP.NET Web API to full fill the request which is working fine. it has an use case that allows user to download user specific secure PDF document. I have implemented this functionality as below

AngularJS:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
 $scope.DownloadHandler = function (id, downloadURL) {

        FileStreamManager.getPdf(id, downloadURL)
             .then(function (result) {
                 // success
                 window.open(downloadURL + id, '_self', '');
             },
                 function (result) {
                     $scope.errors = result.data;
                 });

    };

Note : downloadURL is the Controller call like \ImageRepo\Get

Web Api Controller I have this implementation:

HttpResponseMessage response = new HttpResponseMessage();

            // DB call to to build the URL
            string fileName = "myLocation\Image\doc.pdf";
            if (!fileProvider.Exists(fileName))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            FileStream fileStream = fileProvider.Open(fileName);
            response.Content = new StreamContent(fileStream);
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = fileName;
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response.Content.Headers.ContentLength = fileProvider.GetLength(fileName);
            return response;

Which is working fine. Due to the security issue, i was asked to implement this by using HttpHanlder which is pretty new to me. I have some question on the same.

  1. Should my AngularJS ng-click calls to my .ashx handler directly instead of a Controller URL? Or Should this call route through Controller? How?
  2. I have DB calls to build the image URL and update some the status. Can this be done in Handler itself?
  3. How would i make sure my documents are secured while downloading?

Please help.

2
  • Do you need to configure your handler? Or create it programmatically? Also usually there is some authentication mechanism linked to your controllers and using HttpHandler you'll process requests out of this flow.
    – dr11
    Apr 7, 2015 at 8:57
  • Thanks Deep!!. The question was that, should my angularjs calls the handler directly or through my controller?. I think you answered it that this request getting severed outside of view -> controller flow. Which means that it is a direct call to ashx. The security part i need to work on. Thanks! Apr 7, 2015 at 9:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.