ASP. NET File Upload Overview

Source: Internet
Author: User

1. Upload data to the database.

()
The fields in the database storing files are jimage and the type is image.

The Code defines a variable buf of the byte [] type. In the PostFile of the Upload Component, read the byte array from its InputStream and assign the buf to the data field jimage.

Int len = this. File1.PostedFile. ContentLength;
Byte [] buf = new byte [len];

Stream I = this. File1.PostedFile. InputStream;
I. Read (buf, 0, buf. Length );
News. jimage = buf;
// News is a news class, And jimage is its image attribute, that is, the image in the corresponding table
I. Close ();

Display image:

The image display is also very simple. Note the following in Persister:

SqlDataReader reader = SqlHelper. ExecuteReader ("select jimage from news ");
 
If (reader. Read ())
{
News. jimage = (byte []) reader ["jimage"];
}
Reader. Close ();

To get the content of byte [], it is easy to display it. Add two sentences in the Page_Load () method:

Response. ContentType = "image/jpeg ";
Response. BinaryWrite (ti. content );

In this way, the image can be output. If you want to make some adjustments to the image, such as rotation, conversion format, and image format (jpg or gif), refer to the following code:

// Similarly, the Declaration output is not HTML but image
Response. ContentType = "image/jpeg ";
 
// Obtain an image object from byte []
System. Drawing. Image bmap = Bitmap. FromStream (new MemoryStream (ti. content ));
// Operate on this image
Bmap. RotateFlip (RotateFlipType. Rotate180FlipY );
// Output to the page
Bmap. Save (Response. OutputStream, System. Drawing. Imaging. ImageFormat. Jpeg );
// Release the image
Bmap. Dispose ();

To display an image on an image control, use the following method:

Place an image control at the position of the image and point its src to this page!
For example:
Page: ViewImage. aspx

Put an image control imgZYF on the page where the image is displayed and write it in the code below:
ImgZYF. ImageUrl = "ViewImage. aspx? UserID = "+ userId

Ii. Disk uploaded to the server:

Page file: upload01.aspx

 

 
 


 

Post Code: upload01.aspx

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;

Namespace upload01
{

Public class upload01: System. Web. UI. Page
{
Protected System. Web. UI. HtmlControls. HtmlInputButton Button1;
Protected System. Web. UI. WebControls. TextBox TextBox1;
Protected System. Web. UI. HtmlControls. HtmlInputFile myfile;
 
Private void Page_Load (object sender, System. EventArgs e)
{
// Last night wind www.zuoyefeng.com
}

 

Private void button#serverclick (object sender, System. EventArgs e)
{
// Obtain the client path and file name
String str = myfile. PostedFile. FileName;
// Obtain the desired file, such as. jpg
String filename2 = str. Substring (str. LastIndexOf ("."). ToString (). Trim ();
// Obtain the file size, in K
Double filesize = myfile. PostedFile. ContentLength/1024.00;
// Define the file name in a time scale
String filename1 = DateTime. Now. Ticks. ToString ();
Myfile. PostedFile. SaveAs (Server. MapPath ("/upload01/" + filename1 + filename2 ));
// Save the file name and related information to the database

}
}
}

Upload the file to the disk, and record the file address or path in the table so that it can be referenced in the subsequent program.

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? PostId = 1623366

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.