Image displays images in any absolute path on the server (implemented using a binary stream)

Source: Internet
Author: User

To meet this requirement, the database stores the absolute path of the photo (which may not be in the path of the system), and the Image control dynamically loads the Image in the path.

When I see this question, some people will say that it is not easy. Simply set the URL to an absolute path. I can only say that, if you say this, it only means that you have not thought about it or even figured out what the foreground code and background code mean in Web development, when you do it yourself (not mounted to IIS), The sogou browser can display images (only this one can be displayed, so this method does not make sense ).

The Image control is in the System. web. UI. in the WebControls naming control, the image cannot be directly displayed through byte [] as in winform (I am not quite familiar with this sentence, but can explain it if I understand it ). This method is used to read the Image, write the binary stream to a page, and set the Image URL to this page. The following is my Demo implementation code.
Front-end code:Copy codeThe Code is as follows: <Title> </title>
<Script src = "jquery-1.7.1.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
Function ShowP (obj ){
$ ("# Image1") [0]. src = "ShowPic. aspx? URL = "+ obj. id;
}
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input id = "E: \ 1.jpg" type =" button "value =" button "onclick =" ShowP (this); "/>
<Asp: Image ID = "Image1" runat = "server"/>
</Div>
</Form>
</Body>
</Html>

The front-end code of ShowPic. aspx is empty. The following is the background code of ShowPic. aspx:Copy codeThe Code is as follows: public partial class ShowPic: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
# Region "convert a photo to a binary array based on the photo path"
String strUrl = Request. QueryString ["URL"];
// Read the file in binary mode
FileStream aFile = new FileStream (strUrl, FileMode. OpenOrCreate, FileAccess. ReadWrite );
// Create a binary data stream reader and associate it with the opened file
BinaryReader brMyfile = new BinaryReader (aFile );
// Relocates the file pointer to the beginning of the file
BrMyfile. BaseStream. Seek (0, SeekOrigin. Begin );
// Obtain the byte array of the photo
Byte [] photo = brMyfile. ReadBytes (Convert. ToInt32 (aFile. Length. ToString ()));
// Close all objects of the new object above
BrMyfile. Close ();
# Endregion
Response. BinaryWrite (photo );
}
}

In the above section, I assigned the photo path to the button ID, and then used the path as a parameter to call ShowPic. aspx: In the background code of ShowPic, the photos are written to the page as two-dimensional arrays. If you open this page directly, garbled characters are displayed.

Set the Image Url to this page, and the Image will be displayed. By using the button ID, I realized dynamic loading of images to specific applications. There is also a small question about passing parameters. For JavaScript parameter passing, you need to make your own example, you can use javascript debugging tools to understand the dom structure and then perform operations.

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.