Chandrashekar
11/11/2016 - 8:25 PM

View --> PDF(Rotativa) --> Stream --> Memory Stream --> Email Attachment

View --> PDF(Rotativa) --> Stream --> Memory Stream --> Email Attachment

 //Get: Report
        public ActionResult AuditCompleteReport(long id)
        {
            Models.CurrentUser objCurrentUser = Utility.GetCurrentUser(User.Identity.Name);
            Models.AuditSnapShotReport objAuditSnapShotReport = new Models.AuditSnapShotReport();

            AuditDAL objAuditDal = new AuditDAL();
            objAuditSnapShotReport = objAuditDal.GetAuditSnapShotReport(id, objCurrentUser);

            switch (objAuditSnapShotReport.audit.AuditTypeKey)
            {
                case "LZERO":
                    ViewBag.IsSQISectionEnabled = true;
                    ViewBag.IsSSCISectionEnabled = true;
                    ViewBag.IsFatalSectionEnabled = true;
                    break;
                case "LONE":
                    ViewBag.IsSQISectionEnabled = true;
                    ViewBag.IsSSCISectionEnabled = true;
                    ViewBag.IsFatalSectionEnabled = true;
                    break;
                case "CDQ":
                    ViewBag.IsSQISectionEnabled = true;
                    ViewBag.IsSSCISectionEnabled = false;
                    ViewBag.IsFatalSectionEnabled = false;
                    break;
                case "SRVA":
                    ViewBag.IsSQISectionEnabled = false;
                    ViewBag.IsSSCISectionEnabled = true;
                    ViewBag.IsFatalSectionEnabled = false;
                    break;
            }

            GetMemoryStreamBySaveHttpResponseAsFile("http://16.118.240.129:4455/Report/DownlaodCompleteReportAsPdf/9629", "C:\\test123.pdf");
            return View(objAuditSnapShotReport);
        }

        // GET: Report       
        public ActionResult DownlaodCompleteReportAsPdf(long id)
        {
            Models.CurrentUser objCurrentUser = Utility.GetCurrentUser(User.Identity.Name);
            Models.AuditSnapShotReport objAuditSnapShotReport = new Models.AuditSnapShotReport();

            AuditDAL objAuditDal = new AuditDAL();
            objAuditSnapShotReport = objAuditDal.GetAuditSnapShotReport(id, objCurrentUser);

            switch (objAuditSnapShotReport.audit.AuditTypeKey)
            {
                case "LZERO":
                    ViewBag.IsSQISectionEnabled = true;
                    ViewBag.IsSSCISectionEnabled = true;
                    ViewBag.IsFatalSectionEnabled = true;
                    break;
                case "LONE":
                    ViewBag.IsSQISectionEnabled = true;
                    ViewBag.IsSSCISectionEnabled = true;
                    ViewBag.IsFatalSectionEnabled = true;
                    break;
                case "CDQ":
                    ViewBag.IsSQISectionEnabled = true;
                    ViewBag.IsSSCISectionEnabled = false;
                    ViewBag.IsFatalSectionEnabled = false;
                    break;
                case "SRVA":
                    ViewBag.IsSQISectionEnabled = false;
                    ViewBag.IsSSCISectionEnabled = true;
                    ViewBag.IsFatalSectionEnabled = false;
                    break;
            }

            return new Rotativa.ViewAsPdf("AuditCompleteReport", "_Layout_Blank", objAuditSnapShotReport) { FileName = "AuditCompleteReport" + id.ToString() + ".pdf" };

        }

        public MemoryStream GetMemoryStreamBySaveHttpResponseAsFile(string RequestUrl, string FilePath)
        {
            MemoryStream memStream = new MemoryStream();
            try
            {
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(RequestUrl);
                httpRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
                httpRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
                HttpWebResponse response = null;
                try
                {
                    httpRequest.UseDefaultCredentials = true;
                    httpRequest.PreAuthenticate = true;
                    httpRequest.Credentials = CredentialCache.DefaultCredentials;
                    response = (HttpWebResponse)httpRequest.GetResponse();
                }
                catch (System.Net.WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.ProtocolError)
                        response = (HttpWebResponse)ex.Response;
                }



                using (Stream responseStream = response.GetResponseStream())
                {
                    memStream = new MemoryStream();

                    byte[] buffer = new byte[1024];
                    int byteCount;
                    do
                    {
                        byteCount = responseStream.Read(buffer, 0, buffer.Length);
                        memStream.Write(buffer, 0, byteCount);
                    } while (byteCount > 0);

                    //Stream FinalStream = responseStream;

                    //if (response.ContentEncoding.ToLower().Contains("gzip"))
                    //    FinalStream = new GZipStream(FinalStream, CompressionMode.Decompress);
                    //else if (response.ContentEncoding.ToLower().Contains("deflate"))
                    //    FinalStream = new DeflateStream(FinalStream, CompressionMode.Decompress);

                    //using (var fileStream = System.IO.File.Create(FilePath))
                    //{
                    //    FinalStream.CopyTo(fileStream);
                    //}

                    //response.Close();
                    //FinalStream.Close();
                }

                memStream.Seek(0, SeekOrigin.Begin);

               

                string host = ConfigurationManager.AppSettings["host"].ToString();
                string userName = ConfigurationManager.AppSettings["userName"].ToString();
                string password = ConfigurationManager.AppSettings["password"].ToString();
                string displayName = ConfigurationManager.AppSettings["displayName"].ToString();
                int port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);
                bool enableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["enableSsl"]);

                System.Net.Mail.MailMessage objMailMessage = new System.Net.Mail.MailMessage();
                System.Net.Mail.SmtpClient objSmtpClient = new System.Net.Mail.SmtpClient();

                objSmtpClient.Host = host;
                objSmtpClient.Port = port;
                objSmtpClient.EnableSsl = enableSsl;
                objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                objSmtpClient.UseDefaultCredentials = false;
                //objSmtpClient.Credentials = new NetworkCredential(userName, password);


                objMailMessage.From = new MailAddress(userName, displayName);
                objMailMessage.Priority = System.Net.Mail.MailPriority.Normal;
                objMailMessage.Subject = "Hello";
                objMailMessage.Body = "Hello Content";
                objMailMessage.IsBodyHtml = true;
                objMailMessage.To.Add(new MailAddress("cs.naik@hpe.com"));

                // Create attachment
                ContentType contentType = new ContentType();
                contentType.MediaType = MediaTypeNames.Application.Octet;
                contentType.Name = "fileNameTextBox.pdf";
                Attachment attachment = new Attachment(memStream, contentType);

                // Add the attachment
                objMailMessage.Attachments.Add(attachment);

                objSmtpClient.Send(objMailMessage);
                //return true.ToString();



            }
            catch
            { }

            return memStream;
        }