Paul Rajs

Paul Rajs

  • NA
  • 641
  • 138.6k

How to Download Base64 String as Image in View page in ASP.Net MVC

Jun 25 2021 12:28 PM

Hi Developers,

In my project i have to Download a Base64 string as Image. We using ajax call jquery method to bind datas form WEBSERVICE.

I am having Base64 value in Db and Binding Image displaying fine.

When i am try to Download it comes NoImage.png

1. My Cshtml Code

<a id="aIDproof" class="btn bg-maroon margin">Download</a>
2. Jquery Ajax Call Method

 

function GetCustomerKYCDetails() {    
    var myData =
    {
        MobileNumber: $("#txtMobileNumber").val()
    }

    $.ajax({
        type: "POST",
        url: pathIndex("CustomerDetails") + '/CustomerDetails/GetCustomerKYCDetails',
        dataType: "json",
        data: AddAntiForgeryToken(myData),
--- Success or Failure Response Here --- 
if Success {
// Bind Id_ProofImage image from model
var idProofImageUrl = pathIndex("KYCOperation") + list[0].Id_ProofImagePath;    
    $("#aIDproof").attr("href", idProofImageUrl);
}
3. Controller

public JsonResult CustomerDetails(string MobileNumber)
{
CPKYCVerification objresponse = new CPKYCVerification();
int responseCode = 1;
string message = "Oops! server down";
var responseContent = objresponse.CustomerDetails(MobileNumber, ref responseCode, ref message);
return Json(new { ResponseCode = responseCode, Message = message, ResponseContent = responseContent }, JsonRequestBehavior.AllowGet);
}

4. Model Class

 

[XmlElement(ElementName = "Id_Proof")]
                public string Id_Proof { get; set; }
                public string Id_ProofImagePath
                {
                    get
                    {                        
                        string file = System.Configuration.ConfigurationManager.AppSettings["KYCDocument"] + Id_Proof;
                        string path = clsCommonCryptography.StringEncrypt(string.Format("{0}|{1}", file, 0));
                        path = "/Http/Image?q=" + clsCommonCryptography.Base64ForUrlEncode(path);
                        return path;
                    }
                }

                public string Id_ProofImageExtension
                {
                    get
                    {
                        string _Id_ProofImagePathExtension = "";
                        if (!string.IsNullOrEmpty(Id_Proof))
                        {
                            string[] proof = Id_Proof.Split('.');
                            _Id_ProofImagePathExtension = proof[proof.Length - 1];
                        }
                        return _Id_ProofImagePathExtension;
                    }
                }
5. Model Binding Class

 

public object GetCustomerKYCDetails(string Mobile, ref int responseCode, ref string message)
        {
response = proxy.GetCustomerDetails(request);
                    CustomerDetails.Models.Helper.GeneralHelper.Log_Handler(this.GetType().Name, System.Reflection.MethodBase.GetCurrentMethod().Name, response, 1);
                    response = clsCommonCryptography.StringDecrypt(response);
                    using (TextReader sr = new StringReader(response))
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.LoadXml(response);
                        responseCode = Convert.ToInt32(doc.GetElementsByTagName("STATUSCODE")[0].InnerText);
                        message = doc.GetElementsByTagName("STATUS")[0].InnerText;
                        if (responseCode == 0)
                        {
                            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(CustomerDetails.Areas.KYCOperation.Models.KYCOperation.CPKYCVerification.CustomerKYCVerificationResponse));
                            objResponseContent = (CustomerDetails.Areas.KYCOperation.Models.KYCOperation.CPKYCVerification.CustomerKYCVerificationResponse)serializer.Deserialize(sr);
                            objResponseContent = JsonConvert.SerializeObject(objResponseContent);
                        }
                    }
return objResponseContent;
        }
Kindly verify the codes which am using , if anyone know the solution kindly help me to done this task.

Sorry for brief code because i have tried a lot.

Thanks & Regards,

Paul.S


Answers (2)