Dear Developers,
In my project i need to insert and retrive the image,Imagedata from database. I have tried lot still i did not got exact output.
My Code Cshtml :
@{ ViewBag.Title = "Test"; Layout = "~/Views/Shared/_LoginLayout.cshtml"; }
<h2>Test</h2>
<section style="height:34px;"></section>
@using (Html.BeginForm("InsertImage", "Home", FormMethod.Post, new { id = "__AjaxAntiForgeryForm", @class = "form-horizontal", role = "form" })) { @Html.AntiForgeryToken() <div id="divChangePassword">
<section class="panel"> <div class="panel-body"> <div class="row"> <div class="col-md-8"> <div class="form-horizontal"> <div class=""> <div id="divcbsForm60" class="col-md-6 col-xs-12"> <div class="form-group"> <label> Upload Image <span class="mandatory">* </span><span data-toggle="tooltip" title="You can upload only jpg,png,jpeg,pdf extension file and image size should be less than 5 Mb"><i class="fa fa-info-circle"></i></span>
</label> <input id="uplfilecbsForm60" name="uplfilecbsForm60" title="Form 60" type="file" class="form-control" /> <input id="hdnbytefileuplfilecbsForm60Base64" type="hidden" /> <input id="hdnbytefileuplfilecbsForm60Extension" type="hidden" /> </div> </div> </div> <div> <button id="btnsubmit" type="submit" class="btn btn-info btn-flat pull-right"> <i class="fa fa-save fa-fw"></i>Submit </button> </div> </div> </div>
</div> </div> </section> </div> } @section Scripts { <script src="~/Scripts/Views/Account/Test.js"></script> }
C# Controller
[ValidateInput(false)] [AcceptVerbs(HttpVerbs.Post)] [ValidateAntiForgeryToken] public JsonResult InsertImage(InsertImage model) { int statusCode = 1; string responseContent = string.Empty; if (Request.IsAuthenticated) statusCode = (int)Models.Utilities.Enum.ResponseCode.AuthenticatedTimeout; else { XmlDocument xmldoc = new XmlDocument(); // IDProofImage string iDProofImage = model.ImageName; string ImageData = model.ImageData; if (!string.IsNullOrEmpty(ImageData)) { int indexBase64 = 0, length = 0; indexBase64 = ImageData.LastIndexOf("base64,"); length = ImageData.Length - (indexBase64 + 7); ImageData = ImageData.Substring((indexBase64 + 7), length);
string type = ETCIssuanceCustomerPortal.Models.Utilities.Helper.Helper.GetFileExtension(ImageData); ImageData = string.Format("IDProof_{0}_{1}.{2}", "", DateTime.Now.ToString("ddMMyyyyHHmmsstt"), type); model.ImageData = ImageData; } string lstrRequestXml = glblGeneral.InsertImageRequestXML(model.ImageName, model.ImageData); lstrRequestXml = glblGeneral.EncryptDecrypt(lstrRequestXml, 1); glblGeneral.Log_Handler("Home/InsertImage", lstrRequestXml, 0); string encryptReturnXML = string.Empty; try { string lstrReturnXml = string.Empty; lstrReturnXml = Postman.SendService("InsertImages", lstrRequestXml); encryptReturnXML = lstrReturnXml; lstrReturnXml = glblGeneral.EncryptDecrypt(lstrReturnXml, 2); glblGeneral.Log_Handler("Home/InsertImage", lstrReturnXml, 1); if (!string.IsNullOrEmpty(lstrReturnXml)) { lstrReturnXml = lstrReturnXml.Replace("&", "and"); xmldoc.LoadXml(lstrReturnXml); responseContent = xmldoc.GetElementsByTagName("STATUS")[0].InnerText; if (xmldoc.GetElementsByTagName("STATUSCODE")[0].InnerText == "0") statusCode = 0; } else responseContent = "Oops! please try again later"; } catch (Exception ex) { responseContent = "Oops! please try again later"; glblGeneral.Log_Handler("Home/InsertImage", encryptReturnXML, 1); glblGeneral.Err_Handler("Home/InsertImage", "InsertImage", lstrRequestXml, encryptReturnXML, ex.ToString().Trim()); }
} return Json(new { StatusCode = statusCode, ResponseContent = responseContent, RedirectUrl = Url.Action("Page504", "Http") }, JsonRequestBehavior.AllowGet); }