Posts

Showing posts from April, 2016

File Upload uisng Jquery in C# ASP.Net MVC

There so many ways to upload files with jqueries in ASP.Net C#. One way is with creating XMLHttpRequest and another way is with FormData object. In this post i'll show uploading files and saving form data in one ajax call using jquery. Upload Files with Jquery in ASP.Net MVC C# First code for View which represents normal html code of form @model modelname File Upload File @Html.TextBoxFor(m => m.File, new { @type = "file", @multiple = "multiple" }) Field 1 @Html.TextBoxFor(m => m.Field1, new { @class = "form-control datepicker" }) @Html.ValidationMessageFor(m => m.Field1) Submit MVC Controller Code for Saving File In controller create action method with two arguments one is for formdata and another one is for List of HttpPostedFileBase Class which represents multiple files that you select. public JsonResult SaveFilesWithFormData(ModelName FormData, List Of HTTPPostedFileBase Files) //Model Name rep...

Display image from byte array in ASP.NET MVC

Image
There are many ways to display image from byte array in MVC as well as normal ASP.Net projects.In this post I'll explain couple of them. 1) Using BASE64 string of the image through ViewBag  The first way i s by sending a Base64 string as dataURL through ViewBag. As shown in b elow action method under consideration generates such a Base64 string of the image (often called Data URL) and then pass it to the view via a ViewBag property in MVC . public ActionResult Image() { string path = Server.MapPath("~/images/computer.png"); byte[] imageByteData = System.IO.File.ReadAllBytes(path); string imageBase64Data=Convert.ToBase64String(imageByteData); string imageDataURL= string.Format("data:image/png;base64,{0}", imageBase64Data); ViewBag.ImageData = imageDataURL; return View(); } The above code is a action method of Controller. In this action method it uses physical path of image and read all bytes of that image using ReadAllByte...