Displaying Upload Image in Image Controller in Web API (2024)

Displaying Upload Image in Image Controller in Web API (1)

  • 65.9k
  • 0
  • 1

Introduction

This article explains how to display the uploading image in the image control. First we browse the file and select the image then we click on the show image button, it displays the uploading image.

Use the following procedure to create a sample of the application.

  1. Create an application:
    • Start Visual Studio 2012.
    • From the Start window select "Installed" -> "Visual C#" -> "Web".
    • Select "ASP.NET MVC4 Web Application" and click on the "Ok" button.

      Displaying Upload Image in Image Controller in Web API (3)

    • From the "MVC4 Project" window select "Web API".

      Displaying Upload Image in Image Controller in Web API (4)

    • Click on the "OK" button.
  2. Now select the "HomeController"
    • In the "Solution Explorer".
    • Expand the Controller folder.
    • Select the "HomeController".
    • Displaying Upload Image in Image Controller in Web API (5)

      Add the following code:

      1. usingSystem;
      2. usingSystem.Collections.Generic;
      3. usingSystem.IO;
      4. usingSystem.Linq;
      5. usingSystem.Web;
      6. usingSystem.Web.Mvc;
      7. namespaceMvcApplication3.Controllers
      8. {
      9. publicclassHomeController:Controller
      10. {
      11. publicActionResultIndex()
      12. {
      13. returnView();
      14. }
      15. [HttpPost]
      16. publicActionResultIndex(HttpPostedFileBasefile)
      17. {
      18. //extractonlythefielname
      19. varimageName=Path.GetFileName(file.FileName);
      20. varimgsrc=Path.Combine(Server.MapPath("~/images/"),imageName);
      21. stringfilepathToSave="images/"+imageName;
      22. file.SaveAs(imgsrc);
      23. ViewBag.ImagPath=filepathToSave;
      24. returnView();
      25. }
      26. }
      27. }
  3. Now write the HTMl code in the "index.cshtml" file:

    • In the "Solution Explorer".

    • Expand "Views" folder.

    • Select "Home" -> "index.cshtml".

      Add the following code:

      1. @{
      2. ViewBag.Title="Uploadthefileanddisplaytheuploadedimage";
      3. }
      4. @using(Html.BeginForm("Index","Home",FormMethod.Post,new{enctype="multipart/form-data"}))
      5. {
      6. <div>
      7. SelectImage
      8. <inputtype="file"name="file"/>
      9. <inputtype="submit"value="ShowImage"name="Command"/><br/>
      10. <imgsrc="@ViewBag.ImagPath"style="width:200px;height:200px;"/>
      11. </div>
      12. <div>
      13. </div>
      14. }
  4. Now execute the application.

Displaying Upload Image in Image Controller in Web API (6)

Select the image and click on the "ShowImage" button.

Displaying Upload Image in Image Controller in Web API (7)

Displaying Upload Image in Image Controller in Web API (8)

Displaying Upload Image in Image Controller in Web API (2024)
Top Articles
Top 10 Most Dangerous Cities in the World in 2024
What Is Ravencoin & How Does It Work? Who Created RVN?
Where To Go After Howling Pit Code Vein
Frases para un bendecido domingo: llena tu día con palabras de gratitud y esperanza - Blogfrases
Canya 7 Drawer Dresser
Uti Hvacr
Wordscapes Level 6030
Form V/Legends
Craigslist Campers Greenville Sc
Nyu Paralegal Program
Insidious 5 Showtimes Near Cinemark Tinseltown 290 And Xd
Linkvertise Bypass 2023
RuneScape guide: Capsarius soul farming made easy
Plus Portals Stscg
Acts 16 Nkjv
Jesse Mckinzie Auctioneer
Xrarse
Espn Expert Picks Week 2
How Quickly Do I Lose My Bike Fitness?
Top Hat Trailer Wiring Diagram
10 Great Things You Might Know Troy McClure From | Topless Robot
Ukraine-Russia war: Latest updates
U/Apprenhensive_You8924
Colts Snap Counts
Justified Official Series Trailer
Vistatech Quadcopter Drone With Camera Reviews
Craigslist West Valley
Acts 16 Nkjv
Shiftselect Carolinas
Best Transmission Service Margate
Rimworld Prison Break
Filthy Rich Boys (Rich Boys Of Burberry Prep #1) - C.M. Stunich [PDF] | Online Book Share
Reserve A Room Ucla
Uno Fall 2023 Calendar
Stouffville Tribune (Stouffville, ON), March 27, 1947, p. 1
The Monitor Recent Obituaries: All Of The Monitor's Recent Obituaries
Ofw Pinoy Channel Su
Teenage Jobs Hiring Immediately
About Us | SEIL
Flashscore.com Live Football Scores Livescore
Game8 Silver Wolf
Ticket To Paradise Showtimes Near Regal Citrus Park
Walmart Pharmacy Hours: What Time Does The Pharmacy Open and Close?
All Obituaries | Sneath Strilchuk Funeral Services | Funeral Home Roblin Dauphin Ste Rose McCreary MB
Ig Weekend Dow
Atu Bookstore Ozark
20 Mr. Miyagi Inspirational Quotes For Wisdom
60 Days From August 16
10 Bedroom Airbnb Kissimmee Fl
Haunted Mansion Showtimes Near The Grand 14 - Ambassador
Cataz.net Android Movies Apk
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 5771

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.