C #. NET how to restrict the file format when uploading images

Source: Internet
Author: User
When uploading a file, the file to be uploaded must be in JPG or GIF format. How can this problem be solved?
Source code:
HttpPostedFile hpf = UploadFile. PostedFile;
Try
{
UploadFile. Accept = "images /*";
UpfileName. Text = hpf. FileName;
FileSize. Text = hpf. ContentLength. ToString ();
// Get the file name (excluding the path)
Char [] de = {'\\'};
String [] AFilename = hpf. FileName. Split (de );
String strFilename = AFilename [AFilename. Length-1];
LblFileName. Text = strFilename;
// Save the file to the specified path
Hpf. SaveAs (Server. MapPath ("...") + @ "\ .. \ xwzx \ images_thing \" + strFilename );
String var_path = "\ xwzx \ images_thing \" + strFilename;
UpfileName. Text = strFilename + "uploaded successfully ";
UpfileName. Visible = true;
// Save the path and file name
MyBuExample. UpdateJBJSbyOID (long. Parse (OID. Text. Trim (), var_path );
This. BuilderData ();
}
Catch
{
UpfileName. Text = UploadFile. Value. ToString () + "Upload Failed or error! ";
UpfileName. Visible = true;

String pictype = FileUp. PostedFile. ContentType. ToString (). Replace ("image /","");
If (pictype = "pjpeg ")
{
Pictype = "jpg ";
Suffix = ". jpg ";
}
Else
{
Pictype = "gif ";
Suffix = ". gif ";
}
It is mainly to obtain the image type through debugging and then judge

If (Extname. ToUpper () = ". JPG" | Extname. ToUpper () = ". GIF ")
{
// Upload
}
Else
{
////
}
// Extname indicates the file extension.

Up

If the image is not uploaded, it won't be enough.

This can also be controlled using scripts. If it is not the format you want, upload is not allowed (mainly used: split)

This is actually a false limitation.
If the user changes the name of a file, it is not very good to judge that when a word file is uploaded
It is basically impossible to determine whether it is a real word file.

This function is implemented using client scripts. I use the onpropertychange event to trigger and then determine the file extension. It is better than determining whether to refresh the webpage on the server side.

Check the file header data or simply use an Image to open the file. An error is intercepted.
Otherwise, you can change the suffix of another file and upload it

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.