Display local images on the client and images on the client

Source: Internet
Author: User

Display local images on the client and images on the client

When a user requests a local image on the client, we need to display the local image.

1. Converting images into streams on the Controller side

2. Foreground request Controller method, output image

Controller:

Public class ImageController: Controller {private static readonly string PATH = @ "C: \ Users \ xiechongxi \ Desktop \"; // This key is the image name and suffix public ActionResult GetImage (string key) {// path contains the path and file name, suffix string Path = path. combine (PATH, key); // ext is the extension name string ext = System. IO. path. getExtension (path); // determines whether the path has an image if (System. IO. file. exists (path) {// return an image file using (FileStream fs = System. IO. file. openRead (path) {return File (StreamToBytes (fs), "image/" + ext) ;}} else {return null ;}} private byte [] StreamToBytes (Stream stream) {byte [] bytes = new byte [stream. length]; stream. read (bytes, 0, bytes. length); // set the current stream position to the starting stream of the stream. seek (0, SeekOrigin. begin); return bytes ;}}
Cotroller

Foreground retrieval:

// Image indicates ImageCotroller, and @ scene. LocalKey indicates the file name, including the suffix 
Html

Is it strange in Html?

In fact, you can refer to the following route settings for a beautiful change

Public static void RegisterRoutes (RouteCollection routes) {routes. ignoreRoute ("{resource }. axd/{* pathInfo} "); routes. mapRoute ("Image", // route name "Image/{key}", new {controller = "Image", action = "GetImage"}); routes. mapRoute ("Default", // route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Follow ", action = "Index", id = UrlParameter. optional} // default value );}
Route settings

The new route must be placed on the default route. There is a priority here, so I will not talk about it much.

 

The process is the address next to src in the img label. It is to request the GetImage method (route setting effect) in ImageCotroller, pass the parameters (File Name and suffix), and then the GetImage returns the file.

 

 


Note: This article is for reference only. It also has many flaws. The most important thing is not code, but logic.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.