Upload a small clip

Source: Internet
Author: User
Function:
1. Upload an image file (jpg gif png,
2. Save to the specified path (set the path in web. config and save it in the original file format ),
3. And automatically generate the specified width (set the width in web. config)
4. And the specified format (specify the thumbnail format in web. config)
5. Thumbnail with the same proportion as the original image (calculate the height of the thumbnail Based on the width and height of the original image)
6. You can determine whether a file already exists.
7. If not overwritten, an error is returned.
8. If "Overwrite source image" checkbox is selected, the source image is overwritten.
9. You can set more than one file input and the corresponding checkbox on webform as required.
10. After the file is uploaded, the file name, size, bytes, and
11. The size of the thumbnail file name.
12. File Name format of the thumbnail: the original image + "_ thumb." + specifies the format, for example, test.jpg_thumb.gif, for ease of management.

--------------------
Public void UploadFile (object sender, System. EventArgs e)
{

String imgNameOnly, imgNameNoExt, imgExt;
String imgThumbnail;
Int erroNumber = 0;
System. Drawing. Image oriImg, newImg;
String strFePicSavePath = ConfigurationSettings. receivettings ["FePicSavePath"]. ToString ();
String strFePicThumbFormat = ConfigurationSettings. receivettings ["FePicThumbFormat"]. ToString (). ToLower ();
Int intFeThumbWidth = Int32.Parse (ConfigurationSettings. receivettings ["FePicThumbWidth"]);
String fileExt;

StringBuilder picInfo = new StringBuilder ();

If (Page. IsValid)
{


For (int I = 0; I <Request. Files. Count; I ++)
{
HttpPostedFile PostedFile = Request. Files [I]; // upload multiple Files
FileExt = (System. IO. Path. GetExtension (PostedFile. FileName). ToString (). ToLower (); // get the file extension and convert it to lowercase format

ImgNameOnly = System. IO. Path. GetFileName (PostedFile. FileName); // get the image file name
If (fileExt = ". jpg" | fileExt = ". gif" | fileExt = ". png ")
{

If (System. IO. File. Exists (strFePicSavePath + imgNameOnly) & (checkboxlistRewrite. Items [I]. Selected = false) // The Selected File already Exists
{
ErroNumber = erroNumber + 1;
PicInfo. append ("<B> error: </B> the file (" + (I + 1) + ")" + imgNameOnly + "already exists, modify the file name <br> ");
}
}
Else
{
ErroNumber = erroNumber + 1;
PicInfo. append ("<B> error: </B> file (" + (I + 1) + ") "+ imgNameOnly +" extension "+ fileExt +" not permitted <br> ");
}

}

If (erroNumber> 0)
{
PicInfo. Append ("<font color = red> all operations have not been completed. Please modify the error before performing the operation </font> <br> ");
}
Else
{
For (int I = 0; I <Request. Files. Count; I ++)
{

HttpPostedFile PostedFile = Request. Files [I];
ImgNameOnly = System. IO. Path. GetFileName (PostedFile. FileName );
ImgNameNoExt = System. IO. Path. GetFileNameWithoutExtension (PostedFile. FileName );
ImgExt = System. IO. Path. GetExtension (PostedFile. FileName). ToString (). ToLower ();


OriImg = System. Drawing. Image. FromStream (PostedFile. InputStream); // upload an Image
NewImg = oriImg. GetThumbnailImage (intFeThumbWidth, intFeThumbWidth * oriImg. Height/oriImg. Width, null, new System. IntPtr (0 ));
Switch (imgExt)
{
// Case ". jpeg ":
Case ". jpg ":
OriImg. Save (strFePicSavePath + imgNameOnly, System. Drawing. Imaging. ImageFormat. Jpeg );
Break;
Case ". gif ":
OriImg. Save (strFePicSavePath + imgNameOnly, System. Drawing. Imaging. ImageFormat. Gif );
Break;
Case ". png ":
OriImg. Save (strFePicSavePath + imgNameOnly, System. Drawing. Imaging. ImageFormat. Png );
Break;
}

// OriImg. Save (ConfigurationSettings. deleetask[ "FePicSavePath"] + imgNameNoExt + ". jpg", System. Drawing. Imaging. ImageFormat. Jpeg );

Switch (strFePicThumbFormat)
{
// Jpeg format can get the smallest file size, and the png is the largest size
// Case "jpeg ":
Case "jpg ":
NewImg. Save (strFePicSavePath + imgNameOnly + "_thumb.jpg", System. Drawing. Imaging. ImageFormat. Jpeg );
ImgThumbnail = imgNameOnly + "_thumb.jpg ";
Break;
Case "gif ":
NewImg. Save (strFePicSavePath + imgNameOnly + "_thumb.gif", System. Drawing. Imaging. ImageFormat. Gif );
ImgThumbnail = imgNameOnly + "_thumb.gif ";
Break;
Case "png ":
NewImg. Save (strFePicSavePath + imgNameOnly + "_thumb.png", System. Drawing. Imaging. ImageFormat. Png );
ImgThumbnail = imgNameOnly + "_thumb.png ";
Break;
Default:
NewImg. Save (strFePicSavePath + imgNameOnly + "_thumb.jpg", System. Drawing. Imaging. ImageFormat. Jpeg );
ImgThumbnail = imgNameOnly + "_thumb.jpg ";
Break;

} // Switch

PicInfo. append ("<B> file name: </B>" + imgNameOnly + "(" + oriImg. width + "x" + oriImg. height + ")" + PostedFile. contentLength/1024 + "KB <br> ");
PicInfo. append ("<B> thumbnail name: </B>" + imgThumbnail + "(" + newImg. width + "x" + newImg. height + ") <br> ");

OriImg. Dispose ();
NewImg. Dispose ();

} //
PicInfo. Append ("<font color = red> all operations are successful </font> <br> ");

} // If erronumber = 0



}
Else
{
PicInfo. Append ("<font color = red> An error occurred. Please check. Operation failed </font> <br> ");

}

LblPicInfo. Text = picInfo. ToString ();

}
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.