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
Earnings Before Interest and Taxes (EBIT): Formula and Example
The best LastPass alternatives of 2024: Expert tested
Woodward Avenue (M-1) - Automotive Heritage Trail - National Scenic Byway Foundation
Compare Foods Wilson Nc
Rainbird Wiring Diagram
Costco The Dalles Or
Otis Department Of Corrections
Produzione mondiale di vino
Sinai Web Scheduler
Stream UFC Videos on Watch ESPN - ESPN
LeBron James comes out on fire, scores first 16 points for Cavaliers in Game 2 vs. Pacers
Nexus Crossword Puzzle Solver
Myql Loan Login
Keurig Refillable Pods Walmart
Guilford County | NCpedia
Cambridge Assessor Database
Saatva Memory Foam Hybrid mattress review 2024
Shasta County Most Wanted 2022
Sunset Time November 5 2022
Vernon Dursley To Harry Potter Nyt Crossword
Shoe Station Store Locator
Https E22 Ultipro Com Login Aspx
Snohomish Hairmasters
Xpanas Indo
Big Boobs Indian Photos
San Jac Email Log In
Pioneer Library Overdrive
Helpers Needed At Once Bug Fables
417-990-0201
Landing Page Winn Dixie
Little Caesars Saul Kleinfeld
Emiri's Adventures
Lowell Car Accident Lawyer Kiley Law Group
Bozjan Platinum Coins
Www Craigslist Com Shreveport Louisiana
Retire Early Wsbtv.com Free Book
House Of Budz Michigan
Evil Dead Rise (2023) | Film, Trailer, Kritik
Wayne State Academica Login
Mbfs Com Login
Bustednewspaper.com Rockbridge County Va
Citymd West 146Th Urgent Care - Nyc Photos
Theater X Orange Heights Florida
Ephesians 4 Niv
Diario Las Americas Rentas Hialeah
Smoke From Street Outlaws Net Worth
Spn 3464 Engine Throttle Actuator 1 Control Command
Aaca Not Mine
Craigslist Indpls Free
Appsanywhere Mst
Coors Field Seats In The Shade
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.