Base solution for your next web application
Open Closed

Calling WebApi to download a file #1801


User avatar
0
shyamjith created

Hi,

I am trying to write code for downloading a file . I have a button called Download PDF, which will call an api to generate PDF and send the httpresponse object including the file and details, Here is my code

vm.getPDF = function () {
                    debugger;
                $http.get('api/services/app/user/getPdf', { responseType: "arraybuffer" }).success(function (data, status, headers) {
                    headers = headers();
                    debugger;
                    var filename = headers['x-filename'];
                    var contentType = headers['content-type'];

                    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);
                    } catch (ex) {
                        console.log(ex);
                    }
                }).error(function (data) {
                    console.log(data);
                });
            }
[HttpGet]
        public HttpResponseMessage getPDF()
        {
            HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
            try
            {
                string fileName = "sample.pdf";
                string filePath = HttpContext.Current.Server.MapPath("~/App_Data/") + fileName;

                using (MemoryStream ms = new MemoryStream())
                {
                    using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                    {
                        byte[] bytes = new byte[file.Length];
                        file.Read(bytes, 0, (int)file.Length);
                        ms.Write(bytes, 0, (int)file.Length);


                        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;
            }
            catch (Exception ex)
            {

                return httpResponseMessage;
            }

        }

But I am getting result as null, I have tried this without using the ABP (using angular js, and web api) It works fine, I am attaching both projects here , Please have a look and let me know where it went wrong.


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Which version of ABP do you use ? And is there any exception message in Logs.txt file. You can search your web project folder to find it.

    If there is an error, I think it happens outside of your try-catch block. ABP must handle it an log it into Logs.txt file.