Methods for uploading images in ASP. NET to the MSSQL database and reading images

Source: Internet
Author: User
This article is very simple, just a note of the basic code. So those who already know the method can go out.

Everyone on Earth knows why we need to upload files to the database.

Preparations:
In terms of databases, we need a field of the image type to store the image content.

Step 1: Upload:
I still follow my most common Info-persister method. The attribute type of this field in the info class is byte []. Then, in the postfile of the Upload Component, read the byte array from its inputstream and it will be OK.

1 int Len = This. file1.postedfile. contentlength;
2 byte [] Buf = new byte [Len];
3
4 stream I = This. file1.postedfile. inputstream;
5 I. Read (BUF, 0, Buf. Length );
6 Ti. content = Buf; // an attribute assigned to Info
7 I. Close ();

After obtaining info, you can use persister's create statement to write data to the database.
Because I am relatively lazy and use object [] as the parameter of the stored procedure, rather than the setparameter method.

Step 2:
The image display is also very simple. Note the following in persister:

1 sqldatareader reader = sqlhelper. executereader (constr, "p_hello_get", objs );
2
3 if (reader. Read ())
4 {
5 ti. Name = reader. getstring (1 );
6 Ti. content = (byte []) Reader [2]; // The simplest and quickest way is to do this.
7}
8 reader. Close ();

Of course, if you do not like forced transformation, you can also do this:

1 ti. content = reader. getsqlbinary (2). value;

With the byte [] content, it is easy to display. Add two sentences in the page_load () method:

Response. contenttype = "image/JPEG ";
Response. binarywrite (TI. content );

In this way, the image can be output, but note that if you want to make some adjustments to the image (such as rotation, conversion format, and so on ), you can also know the image format (JPG or GIF:

1 // Similarly, declare that the output is not HTML but Image
2 response. contenttype = "image/JPEG ";
3
4 // obtain an image object from byte []
5 system. Drawing. Image bmap = bitmap. fromstream (New memorystream (TI. Content ));
6 // operate on this image
7 bmap. rotateflip (rotatefliptype. rotate180flipy );
8
9 // output to the page
10 bmap. Save (response. outputstream, system. Drawing. imaging. imageformat. JPEG );
11
12 // release the image
13 bmap. Dispose ();
14

Of course, I will not say much about how to dynamically display images in ASP. It is nothing more than making

 

 

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.