Download csv file in client side with mvc controller

1) This is controller action method
public ActionResult FeedbackExport(List<string> guidList)
        {
            string fileName = "CDLmallFeedback.csv";
            List<ContactUsViewModel> feedbackFullList = new ContactUsManager().GetAllFeedbackHistory().ToList();

            if (guidList != null && guidList.Count() > 0)
            {
                feedbackFullList = feedbackFullList.Where(x => guidList.Contains(x.GUID)).ToList();
            }

            string delimiter = ",";
            int feedbackLength = feedbackFullList.Count();
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Join(delimiter, "Name", "Gender", "Email", "Contact No", "Status", "Message Type", "Subject", "Mall Type", "Message from", "Date time", "Message"));

            for (int index = 0; index < feedbackLength; index++)
            {
                int chatCount = feedbackFullList[index].ContactChatHistoryList.Count();

                for (int i = 0; i < chatCount; i++)
                {
                    string message = feedbackFullList[index].ContactChatHistoryList[i].Message.Replace(',', ' ').Replace("\n", " ").Replace("\r", " ");
                    if (i == 0)
                    {
                        sb.AppendLine(string.Join(delimiter, feedbackFullList[index].Customer, feedbackFullList[index].Gender, feedbackFullList[index].Email, feedbackFullList[index].ContactNumber, feedbackFullList[index].Status, feedbackFullList[index].MessageType, feedbackFullList[index].Subject, feedbackFullList[index].MallType, feedbackFullList[index].ContactChatHistoryList[i].ResponderName, feedbackFullList[index].ContactChatHistoryList[i].RespondOnDateTimeText, message));
                    }
                    else
                    {
                        sb.AppendLine(string.Join(delimiter, " ", " ", " ", " ", " ", " ", " ", " ", feedbackFullList[index].ContactChatHistoryList[i].ResponderName, feedbackFullList[index].ContactChatHistoryList[i].RespondOnDateTimeText, message));
                    }
                }

            }
            try
            {
                 return File(new System.Text.UTF8Encoding().GetBytes(sb.ToString()), "text/csv", fileName);         
                //return Json(sb.ToString(), JsonRequestBehavior.AllowGet);
            }
            catch (Exception e)
            {

                return Json(e.Message, JsonRequestBehavior.AllowGet);
            }
        }

2) This is view code

 $scope.feedbackExport = function () {

        var selectedGuid = []
        $("input[name='checkFeedback']:checked").each(function () {
            selectedGuid.push($(this).attr('value'));
        })

        if (selectedGuid.length > 0)
        {
            var feedbackExportVar = contactUsService.feedbackExportApi(selectedGuid); //$scope.contactUsList.map(function (a) { return a.GUID })
            feedbackExportVar.then(function (result) {
                
                var blob = new Blob([result.data], { type: "text/csv;charset=utf-8;" });
                var filename = 'CDLmall-' + $scope.mallType + '-FeedbackCSV.csv';

                if (navigator.msSaveBlob)
                { // IE 10+
                    navigator.msSaveBlob(blob, filename)
                }
                else
                {
                    var link = document.createElement("a");
                    if (link.download !== undefined) {
                        var url = URL.createObjectURL(blob);
                        link.setAttribute("href", url);
                        link.setAttribute("download", filename);
                        link.style = "visibility:hidden";
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                    }
                }

            }, function (error) {
                $scope.error = error.data.MessageDetail;
                });
        } else
        {
            alert('Please select at least one feedback ')

        }

Comments

Post a Comment

Popular posts from this blog

How can we find .net version in server or computer

Develop recaptcha validation in c# backend and website