#Expected Output
#Controller Code(TestController.cs)
#Model (ProductViewModel.cs)
# View (Index.cshtml)
#Controller Code(TestController.cs)
using MVCTutorial.Models; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Threading; using System.Web; using System.Web.Mvc; namespace MVCTutorial.Controllers { public class TestController : Controller { public ActionResult Index() { return View(); } public JsonResult ImageUpload(ProductViewModel model) { var file = model.ImageFile; if (file != null) { var fileName = Path.GetFileName(file.FileName); var extention = Path.GetExtension(file.FileName); var filenamewithoutextension = Path.GetFileNameWithoutExtension(file.FileName); file.SaveAs(Server.MapPath("/UploadedImage/" + file.FileName)); } return Json(file.FileName, JsonRequestBehavior.AllowGet); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MVCTutorial.Models { public class ProductViewModel { public string ProductName { get; set; } public string Title { get; set; } public string Description { get; set; } public Nullable<int> Price { get; set; } public Nullable<int> ImageId { get; set; } public HttpPostedFileWrapper ImageFile { get; set; } } }
@model MVCTutorial.Models.ProductViewModel @{ ViewBag.Title = "Index"; // Layout = null; } <div class="panel panel-body" style="min-height:256px"> @*<div class="col-md-3"> @{ Html.RenderAction("SideMenu", "Test");} </div>*@ <div class="col-md-9"> <div class="col-md-4"> <div class="btn btn-primary"> <input type="file" id="imageBrowes" /> </div> <hr /> <div id="imgPreview" class="thumbnail" style="display:none"> <img class="img-responsive" id="targetImg" /> <div class="caption"> <a href="#" onclick="ClearPreview()"><i class="glyphicon glyphicon-trash"></i></a> <span id="description"></span> </div> <a href="#" class="btn btn-default" onclick="Uploadimage()">Upload</a> </div> </div> <div class="col-md-2 thumbnail" id="uploadedImage" > </div> </div> </div> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script> $(document).ready(function () { $("#imageBrowes").change(function () { var File = this.files; if (File && File[0]) { ReadImage(File[0]); } }) }) var ReadImage = function (file) { var reader = new FileReader; var image = new Image; reader.readAsDataURL(file); reader.onload = function (_file) { image.src = _file.target.result; image.onload = function () { var height = this.height; var width = this.width; var type = file.type; var size = ~~(file.size / 1024) + "KB"; $("#targetImg").attr('src', _file.target.result); $("#description").text("Size:" + size + ", " + height + "X " + width + ", " + type + ""); $("#imgPreview").show(); } } } var ClearPreview = function () { $("#imageBrowes").val(''); $("#description").text(''); $("#imgPreview").hide(); } var Uploadimage = function () { var file = $("#imageBrowes").get(0).files; var data = new FormData; data.append("ImageFile", file[0]); data.append("ProductName", "SamsungA8"); $.ajax({ type: "Post", url: "/Test/ImageUpload", data: data, contentType: false, processData: false, success: function (response) { ClearPreview(); $("#uploadedImage").append('<img src="/UploadedImage/' + response + '" class="img-responsive thumbnail"/>'); } }) // } </script>
# Layout (_Layout.cshtml)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@ViewBag.Title - My ASP.NET Application</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") </head> <body> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="/Test/Index"> <img alt="Brand" src="~/Content/Images/logo.png" height="20" width="20"> </a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li><a href="#"><span class="glyphicon glyphicon-home"></span></a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Services <span class="caret"></span></a> <ul class="dropdown-menu" role="menu" id="myTab"> <li role="presentation"><a href="#music" aria-controls="music" role="tab" data-toggle="tab"><span class="glyphicon glyphicon-music"> </span> Music & Dance</a></li> <li class="divider"></li> <li role="presentation"><a href="#training" aria-controls="training" role="tab" data-toggle="tab"><span class="glyphicon glyphicon-book"> </span> Summar Training</a></li> <li class="divider"></li> <li role="presentation"><a href="#website" aria-controls="website" role="tab" data-toggle="tab"><span class="glyphicon glyphicon-cloud"> </span> Website Building</a></li> </ul> </li> <li><a href="#">About Us</a></li> <li><a href="#">Career</a></li> </ul> <form class="navbar-form navbar-left" role="search"> <div class="form-group"> <input type="text" class="form-control" placeholder="Search"> </div> <button type="submit" class="btn btn-default">Submit</button> </form> @if (Session["UserId"] != null) { <ul class="nav navbar-nav navbar-right"> <li><a href="#">Welcome :@Session["UserName"] </a></li> <li><a href="/Test/Logout">Logout</a></li> </ul> } else { <ul class="nav navbar-nav navbar-right"> <li><a href="/Test/Login">Login</a></li> <li><a href="/Test/Registration">Register</a></li> </ul> } } </div> </div> </nav> <div class="panel-body" style="min-height:450px"> @RenderBody() </div> <div class="well" style="background-color:#000000;color:wheat;margin-bottom:0px"> <div class="col-lg-4" style="margin-left:20px"> <h3><span class="glyphicon glyphicon-map-marker"></span> Address</h3> <ul class="list-group list-unstyled"> <li>Technotips Ltd.</li> <li>xyz Industrial Area</li> <li>Phase-2,New Delhi-52</li> </ul> </div> <div class="col-lg-4"> <h3><span class="glyphicon glyphicon-cog"></span> Our Services</h3> <ul class="list-group list-unstyled"> <li><span class="glyphicon glyphicon-music"></span> <a href="#">Musical & Dance Traning</a> </li> <li><span class="glyphicon glyphicon-book"></span> <a href="#">Training in Asp.Net,Java,android etc.</a></li> <li><span class="glyphicon glyphicon-globe"></span> <a href="#">Web designing -corporate & Domestic</a> </li> </ul> </div> <div class="col-lg-3"> <h3><span class="glyphicon glyphicon-briefcase"></span> Career</h3> <ul class="list-group list-unstyled"> <li><span class="glyphicon glyphicon-music"></span> <a href="#">Musical & Dance</a> </li> <li><span class="glyphicon glyphicon-book"></span> <a href="#">Summar training</a></li> <li><span class="glyphicon glyphicon-globe"></span> <a href="#">Website Designing</a></li> </ul> </div> <div style="clear:both"></div> </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") <script src="~/Scripts/jquery.validate.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> @RenderSection("scripts", required: false) </body> </html>
All Code Factory
- Part 11- Insert data into database
- Part 12- Server side and clientside validation
- Part 13- Insert data into multiple tables
- Part 14- Insert data into database using JQuery
- Part 15- How to create Bootstrap Popup
- Part 16- Delete operation in Asp.net MVC
- Part 17- What is Partial View in Asp.net MVC
- Part 18- How to call Partial View using JQuery
- Part 19- Difference between Html.Partial() and Html.RenderPartial()
- Part 20- AddEdit Record using Partial View
- Part 21- Layout View in Asp.net MVC
- Part 22- Style.Render and Script.Render
- Part 23 - RenderBody, RenderSection and RenderPage.
- Part 24- Divide Page into several component using Bootstrap
- Part 25- Refresh Entity framework after any modification in database table
- Part 26- Set foreign key relationnship in database tables
- Part 27- Create Rgistration Page
- Part 28- Create Login Page
- Part 29- Client Side Validation using JQuery
- Part 30- How to return multiple Model to a View (Interview)
- Part 31- How to create Dynamic Menu using Partial View
- Part 32- Preview Image Before Uploading
- Part 33- Upload and Display Image using JQuery
- Part 34-Upload Image to SQL Server and Display
- Part 35- Download Image from URL and Upload to SQL Server
- Part 36- Cascading DropdownList
- Part 37- Implement Search Functionality
- Part 38- Attribute Routing in MVC
- Part 39- How to display multiple checkbox checked data
- Part 40- How to send multiple checkbox checked value to Server
- Part 41- How to create responsive sortable Image Gallery
- Part 42 - How to implement JQuery Autocomplete Textbox
- Part 43 - How to send Emails in Asp.net MVC
- Part 44 - Integrate JQuery DataTables plugin
- Part 45 - Display record from database using JQuery Datatable
- Part 46- Add Edit Record using JQuery DataTable
- Part 47 - JQuery DataTables Server -side Processing
- Part 48 - JQuery server side processing -Search functionality
- Part 49 - Pagination using Skip and Take method
- Part 50 - Refresh DataTable After Performing Any Action
- Part 51 - Send OTP ( One Time Password ) to any mobile device
- Part 52 - How to use AutoMapper in Asp.net MVC
- Part 53 - How to use AutoMapper ForMember Method
- Part 54 - Repository Pattern - 1 - Adding Business Layer
- Part 55 - Repository Pattern - 2 - Adding Domain Layer
- Part 56 - Repository Pattern - 3 - Dependency Injection
- Part 57- Repository Pattern- 4 - Adding Data Access Layer
- Part 58 - Repository Pattern - 5 - Setting Up Generic Repository
- Part 59 - Display Record using repository pattern
- Part 60 - Add Edit Record using Repository Pattern
7 comments:
Hi
Thanks for the great tutorial. I would like your further on how I can set the uploaded image as background image using javascript. That is getting the file that has been uploaded into the file system and setting it as a background image.
Thank you
How to edit or update the image using ajax jquery?
Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
python training in chennai
python course in chennai
Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage contribution from other ones on this subject while our own child is truly discovering a great deal. Have fun with the remaining portion of the year.
Data Science training in chennai
Data Science training in OMR
Data Science training in chennai
Data Science Training in Chennai
Data Science training in Chennai
Data Science training in anna nagar
This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.
Best Devops Training in pune
Microsoft azure training in Bangalore
Power bi training in Chennai
Jabardast hai jawab nahi
I got to know about the blog by my friend's reference.If anyone wants to read a good content...?Then this blog will be the answer for you.Such a worthy content out of it. DevOps Training | Certification in Chennai | DevOps Training | Certification in anna nagar | DevOps Training | Certification in omr | DevOps Training | Certification in porur | DevOps Training | Certification in tambaram | DevOps Training | Certification in velachery
Post a Comment