How to upload electronic photos in ASP. NET

Source: Internet
Author: User

Currently, many applications have encountered the upload of Electronic photos or images. Let's take a look at how to implement this function in. net.

// Upload an electronic photo that is not saved to the database. It is saved to the session and displayed on the image control.
Private void btn_upload_click (Object sender, system. eventargs E)
{
// Before each photo upload, clear the content in the session ["imagedata"], session ["imagesize"] And session ["tempimage "].
Session ["imagedata"] = NULL;
Session ["imagesize"] = NULL;
Session ["tempimage"] = NULL;

Httppostedfile upfile = This. up_file.postedfile;
Int ifilelength = upfile. contentlength;
String imagetype;
Try
{
If (upfile. filename. Equals (""))
{
Imagetype = "";
}
Else
{
Imagetype = upfile. filename. substring (upfile. filename. lastindexof ('. '), upfile. filename. length-upfile. filename. lastindexof ('. ')). trim (). tolower ();
}
If (ifilelength = 0)
{
Response. Write ("<script language = JavaScript> ");
Response. Write ("alert ('select the file to upload first! ');");
Response. Write ("</SCRIPT> ");
Return;
}
Else if (ifilelength & gt; 1048576)
{
Response. Write ("<script language = JavaScript> ");
Response. Write ("alert ('the file to be uploaded is too large, and the maximum system limit is 1 MB! ');");
Response. Write ("</SCRIPT> ");
Return;
}
Else if (this. tb_gh.text = "")
{
Response. Write ("<script language = JavaScript> ");
Response. Write ("alert ('enter the employee ID first! ');");
Response. Write ("</SCRIPT> ");
Return;
}
Else if (imagetype! = ". Jpg" & imagetype! = ". BMP" & imagetype! = ". GIF ")
{
Response. Write ("<script language = JavaScript> ");
Response. Write ("alert ('the image format is incorrect. jpg format is required! ');");
Response. Write ("</SCRIPT> ");
Return;
}
Else
{
This. image1.imageurl = This. up_file.postedfile.filename;

If (this. image1.width. value> 100 | this. image1.height. value> 120)
{
This. image1.imageurl = "";
Response. Write ("<script language = JavaScript> ");
Response. Write ("alert ('the size of the uploaded photo is too large! The size is limited to 100*120! ');");
Response. Write ("</SCRIPT> ");
Return;
}
Else
{
This. lb_msg.visible = true;
This. lb_msg.text = "the electronic photo has been uploaded! ";

Byte [] filebytearray = new byte [ifilelength];
Stream streamobject = upfile. inputstream;
Int imgsize = upfile. contentlength;
String imgtype = upfile. contenttype;
String imgid = This. tb_gh.text.trim ();

Session ["tempimage"] = (httppostedfile) upfile;

// Assign the image information to the session again
Session ["imagedata"] = (byte []) filebytearray;
Session ["imagesize"] = convert. toint32 (imgsize. tostring ());
}

}
}
Catch (exception ex)
{
This. lb_msg.visible = true;
This. lb_msg.text = ex. Message. tostring ();
}
}
In this example, the image is saved to the data warehouse in the form of a binary byte character, read the binary character first, and then write it into a temp.jpg file for display,CodeAs follows:

//Create a temp.jpg file. If the file already exists, rewrite the file as an empty file.
Filestream FS = new filestream (server. mappath (".. \ image \ temp.jpg"), filemode. Create );
FS. Write (byte []) read ["lzb2dzzp"], 0, convert. toint32 (read ["lzb2imagesize"]);
This. image1.imageurl = ".. \ image \ temp.jpg ";

FS. Close ();
Session ["imagedata"] = (byte []) read ["lzb2dzzp"];
Session ["imagesize"] = convert. toint32 (read ["lzb2imagesize"]);

You can also save images to the file directory, which may be better in terms of reading and storage efficiency.

The problem is: if you select to display a photo of the database (or directory) for the first query, the image will be displayed normally. The second query will be performed again, then, select "B" to display the photo of "A". You need to refresh the image once to display the photo of "B". This problem cannot be solved by yourself, but may be related to the cache, but it is not clear where the specific problem is. If anyone can provide a good solution, I hope you can leave a message here.

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.