Click here to Skip to main content
15,867,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to download file but there is no content present after download.Please assist me to resolve it.Below is my code.

WebApi Code
-------------
[Authorize]
        [HttpPost]
        [Route("downloadStaticDocument")]
        public HttpResponseMessage downloadStaticDocument(OrderItemReview order)
        {

            HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
            string fileName = "";
            string fileExtension = "";
            byte[] dataStream = new byte[0];
            string downloadName = order.itemDetails.staticDocumentName;
            try
            {
                Log.Debug("starting downloadStaticDocument");
                dataStream = Convert.FromBase64String(order.itemDetails.staticDocumentBase64);
                Log.Debug("fetching file names");
                fileName = Path.GetFileNameWithoutExtension(downloadName);
                fileExtension = Path.GetExtension(downloadName);
                Log.Debug("fileName : " + fileName);
                Log.Debug("fileExtension : " + fileExtension);

                httpResponseMessage.Content = new ByteArrayContent(dataStream.ToArray());
                httpResponseMessage.Content.Headers.Add("x-filename", fileName);
                //httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(GetMimeType(fileExtension));
                httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");

                string contentType = string.Empty;
                string ext = Path.GetExtension(downloadName).ToLower();
                switch (ext)
                {
                    case ".pdf":
                        contentType = "application/pdf";
                        break;
                    case ".doc":
                    case ".docx":
                        contentType = "application/msword";
                        break;
                    case ".xls":
                    case ".xlsx":
                        contentType = "application/vnd.ms-excel";
                        break;
                    case ".txt":
                        contentType = "text/plain";
                        break;
                    case ".jpg":
                        contentType = "image/JPEG";
                        break;
                    case ".png":
                        contentType = "image/PNG";
                        break;
                    case ".rtf":
                        contentType = "application/rtf";
                        break;
                    default:
                        break;
                }


                httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);

                return httpResponseMessage;
            }
            catch (Exception ex)
            {
                Log.Error(ex);

                return this.Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
            }
        }


Angularjs Service calling code
--------------------
var _downloadStaticDocument = function (orderDetails) {
    
        debugger;
        var request = $http({
            method: "post",
            url: serviceBase + "api/orders/downloadStaticDocument",
            data: orderDetails,
            responseType: 'arraybuffer'
            
        });
        return request;
    }


AngularJs download function code
---------------------------------
$scope.downloadStaticDocument = function (orderDetails) {
        debugger;
        $rootScope.isLoading = true;
        $scope.ProcessFileName = orderDetails.itemDetails.productFullName + ".pdf";

        var promise = ordersManagementService.downloadStaticDocument(orderDetails);
            promise.then(function (response) {

                //for IE Compatibility to dowload blob data
                var anchor = angular.element('<a/>');
                var blob = new Blob([response.data]);
                var fileName = orderDetails.itemDetails.productName;
                if (window.navigator && window.navigator.msSaveOrOpenBlob) {
                    window.navigator.msSaveOrOpenBlob(blob);
                    $rootScope.isLoading = false;
                } else {
                    anchor.attr({
                        href: window.URL.createObjectURL(blob),
                        target: '_blank',
                        download: $scope.ProcessFileName
                    })[0].click();
                    $rootScope.isLoading = false;
                }
            
            ngToast.create({
                content: fileName + " Downloaded successfully.",
                className: 'success',
                dismissOnTimeout: true,
                timeout: 6000
            });

        }, function (error) {
            ngToast.create({
                content: error.data.message,
                className: 'error',
                dismissOnTimeout: true,
                timeout: 6000
            });
        });

    }


What I have tried:

I am not able to identify the cause.Can you please some one help me on it
Posted
Comments
Mehul M Thakkar 9-Dec-19 9:49am    
have you tried to debug and see the errors?
Sambit_Sahoo 9-Dec-19 13:44pm    
Yes ..I have tried and I am suspecting like in the below line
$scope.ProcessFileName = orderDetails.itemDetails.productFullName + ".pdf";

because when I am changing the above line to like below
$scope.ProcessFileName = orderDetails.itemDetails.productFullName + ".docx"; then it is downloading the docx file.
So please help me to write a condition to check the file type before downloading.If any additional info you want suggest it is highly appriciated.
Sambit_Sahoo 10-Dec-19 6:03am    
Can any body please look into this issue

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