Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I tried from the following way..plz get me idea where i am wrong.....

c#
....
C#
byte[] bytes = memoryStream.ToArray();
                    memoryStream.Close();
httpResponseMessage.Content = new ByteArrayContent(bytes.ToArray());
                    httpResponseMessage.Content.Headers.Add("x-filename", fileName);
                    httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                    httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                    httpResponseMessage.Content.Headers.ContentDisposition.FileName = fileName;
                    httpResponseMessage.StatusCode = HttpStatusCode.OK;
                    return httpResponseMessage;

in angularjs

JavaScript
$http({
           method: 'post',
           url: basePath + '/profile/downloadpdf_fromedit',
          // data: JSON.stringify($scope.paginginfostmntaccnt),
           responsetype: 'arraybuffer',
           headers: {'content-type': 'application/pdf'},
           //  headers: { 'content-type': 'application/json' }
       })
           .then(function (response) {
             //  console.log(response.data);

               $scope.dataDetail = response.data;
               console.log($scope.dataDetail)

1.               var file = new Blob([response.data], { type: 'application/pdf' });

saveAs(file, 'StatementofAccount.pdf');


//url-file:///C:/Users/tushar/Downloads/StatementofAccount.pdf
//failed to load pdf

JavaScript
2.var file = new Blob([response], { type: 'application/pdf' });

  var fileurl = URL.createObjectURL(file);
                window.open(fileurl);


//url- blob:http://localhost:16311/02f8d85e-74c0-4ccd-b937-22f02cc3866c

//failed to load pdf document


3.
JavaScript
.success(function (data, status, headers, config) {
            // any required additional processing here 
            var results = [];
            results.data = data;
            results.headers = headers();
            results.status = status;
            results.config = config;

            console.log(results)

                $("#loading").hide();
                headers = headers();

                var filename = headers['x-filename'];

                var contentType = headers['content-type'];
                if (!filename) {
                    filename = headers["x-filename"] || 'statementAccount.pdf';
                }

                var linkElement = document.createElement('a');
                try {
                    var blob = new Blob([data], { type: contentType });
                    var url = window.URL.createObjectURL(blob);

                    linkElement.setAttribute('href', url);
                    linkElement.setAttribute("download", filename);

                    var clickEvent = new MouseEvent("click", {
                        "view": window,
                        "bubbles": true,
                        "cancelable": false
                    });
                    linkElement.dispatchEvent(clickEvent);
                    $("#loading").hide();

                    console.log("filename" + filename);

                } catch (ex) {
                    console.log("catch"+ex);
                    $("#loading").hide();
                }
            })

//url-file:///C:/Users/tushar/Downloads/statementAccount.pdf
//failed to load pdf document

What I have tried:

The code I have tried i have included in query.
Posted
Updated 28-Sep-16 4:13am
v2

1 solution

This link has an approach that I would recommend you try.

c# - Download file from an ASP.NET Web API method using AngularJS - Stack Overflow[^]

Even though it says Web Api the code can still be applied to an MVC action I am sure.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900