Wednesday 26 July 2017

Bytes save and download as PDF in web API


In this obj.ValueType is a base 64 data Firstly we have to convert base 64 into Byte[] after that save in a folder or also direct download without saving the pdf file.


  public HttpResponseMessage updateprofileImage(info obj)
        {
            var httpRequest = HttpContext.Current.Request;
            string fileOriginalPath = "~/FileUploaded";
            var file64 = obj.ValueType;// httpRequest.Form[0];

            string strTutorImg = string.Empty;

            if (!string.IsNullOrEmpty(file64))
            {
                /*where you need to save 64 file*/
                string Base64file;
                Base64file = DateTime.UtcNow.Ticks + "-" + "base64-" + "decod.pdf";
                var filepath1 = httpRequest.MapPath(fileOriginalPath);
                /*end here */

                /*Convert base64 file into pdf file start here*/
                Byte[] bytes2 = Convert.FromBase64String(file64);

                //save into folder
                filepath1 = Path.Combine(filepath1, System.IO.Path.GetFileName(Base64file));
                File.WriteAllBytes(filepath1, bytes2);

                //direct download 
                HttpContext.Current.Response.Clear();
                MemoryStream ms = new MemoryStream(bytes2);
                HttpContext.Current.Response.ContentType = "application/pdf";
                HttpContext.Current.Response.AddHeader("content-disposition",                 "attachment;filename=labtest.pdf");
                HttpContext.Current.Response.Buffer = true;
                ms.WriteTo(HttpContext.Current.Response.OutputStream);
                HttpContext.Current.Response.End();

             
                /*End here*/

                /*if you need to save this base64 into database so save this variable value=>   file64*/

            }
            return Request.CreateResponse(HttpStatusCode.OK, file64); ;
        }

Monday 24 July 2017

Base64 Encode and Decode any file in web api.


Base64 Encode and Decode  any file using Postman and web API





namespace
===============================
using System.Net.Http;
using System.Web;
using System.Web.Http;



Action
===============================
        [HttpPost]
        public HttpResponseMessage updateprofileImage()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            var httpRequest = HttpContext.Current.Request;
            string FileName1 = string.Empty;
            string base64 = string.Empty;
            String file64 = string.Empty;
            if (httpRequest.Files.Count > 0)
            {
                HttpPostedFile file = httpRequest.Files[0];
                string fileOriginalPath = string.Empty;
                fileOriginalPath = "~/FileUploaded";
                string strTutorImg = string.Empty;

                if (file != null)
                {
                    string Customerfile;
                    Customerfile = DateTime.UtcNow.Ticks + "-" + 'C' + "-" + file.FileName;

                    var filepath = httpRequest.MapPath(fileOriginalPath);
                    var base63 = httpRequest.MapPath(base64);
                    if (!Directory.Exists(filepath))
                        Directory.CreateDirectory(filepath);
                    filepath = Path.Combine(filepath, System.IO.Path.GetFileName(Customerfile));
                    file.SaveAs(filepath);

                    /*Convert Pdf into base64 formate start here(Encode)*/
                    Byte[] bytes = File.ReadAllBytes(filepath);
                    file64 = Convert.ToBase64String(bytes);
                    /*end here*/

                    /*where you need to save 64 file*/
                    string Base64file;
                    Base64file = DateTime.UtcNow.Ticks + "-" + "base64-" + "decod.pdf";
                    var filepath1 = httpRequest.MapPath(fileOriginalPath);
                    filepath1 = Path.Combine(filepath1, System.IO.Path.GetFileName(Base64file)); 
                    /*end here */

                    /*Convert base64 file into pdf file start here(Decode)*/
                    Byte[] bytes2 = Convert.FromBase64String(file64);               
                    File.WriteAllBytes(filepath1, bytes2);
                    /*End here*/


                    FileName1 = file.FileName;
                    var CustomerImage1 = Customerfile;
                }
            }
            return Request.CreateResponse(HttpStatusCode.OK, file64); ;
        }

Pass Base64 data(Img,pdf,etc) into web API using Postman.




Decode base64 to pdf in web API





Web API:=>


NameSpace
==========================================================
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

==========================================================

        #region Upload Base64 formate and save into database and folder.
        [HttpPost]
        public HttpResponseMessage updateprofileImage()
        {          
                var httpRequest = HttpContext.Current.Request;  
                string fileOriginalPath = "~/FileUploaded";
                var file64 = httpRequest.Form[0];
           
                string strTutorImg = string.Empty;
           
                if (!string.IsNullOrEmpty(file64))
                {                        
                    /*where you need to save 64 file*/
                    string Base64file;
                    Base64file = DateTime.UtcNow.Ticks + "-" + "base64-" + "decod.pdf";
                    var filepath1 = httpRequest.MapPath(fileOriginalPath);
                    filepath1 = Path.Combine(filepath1, System.IO.Path.GetFileName(Base64file));
                    /*end here */

                    /*Convert base64 file into pdf file start here*/                  
                    Byte[] bytes2 = Convert.FromBase64String(file64);                   
                    File.WriteAllBytes(filepath1, bytes2);
                /*End here*/

                /*if you need to save this base64 in to database so save  this variable value=>   file64*/

            }
            return Request.CreateResponse(HttpStatusCode.OK, file64); ;
        }

        #endregion