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