Fileupload图像类型验证C#asp.net [英] Fileupload image types validation C# asp.net
本文介绍了Fileupload图像类型验证C#asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我在验证文件上传类型时遇到问题。其余的工作正常,但我不知道如何验证文件类型。
以下是我验证文件类型的部分:
Hello, i have problems with validating the file upload types. The rest are working fine but i have no idea how to validate the file types.
Below is the part where i validate my file types:
private void StartUpLoad()
{
string companyID = this.Label1.Text;
string imgName = FileUpload1.FileName;
string imgPath = "~/Uploads/" + imgName;
int imgSize = FileUpload1.PostedFile.ContentLength;
string ext = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName);
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
if (FileUpload1.PostedFile.ContentLength > 1000000)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);
}
if (ext != ".jpg" || ext != ".png" || ext != ".gif" || ext !=".jpeg")
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Please choose only .jpg, .png and .gif image types!')", true);
}
else
{
FileUpload1.SaveAs(Server.MapPath(imgPath));
Image1.ImageUrl = imgPath;
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);
byte[] theImage = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(theImage, 0, (int)FileUpload1.PostedFile.ContentLength);
int length = theImage.Length;
string fileName = FileUpload1.FileName.ToString();
string type = FileUpload1.PostedFile.ContentType;
int size = FileUpload1.PostedFile.ContentLength;
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
WP_advBLL canwork = new WP_advBLL();
canwork.ExecuteInsert(theImage, type, size, fileName, length, companyID);
Response.Write("File has been uploaded successfully!");
Session["imageID"] = imgName;
}
}
}
粗线是文件类型的验证,但它不管用。知道为什么吗?
the bold lines are the validation for file types, but it is not working. any idea why?
推荐答案
private void StartUpLoad()
{
string companyID = this.Label1.Text;
string imgName = FileUpload1.FileName;
string imgPath = "~/Uploads/" + imgName;
int imgSize = FileUpload1.PostedFile.ContentLength;
string ext = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName).ToLower();
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
if (FileUpload1.PostedFile.ContentLength > 1000000)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);
}
if (ext != ".jpg" || ext != ".png" || ext != ".gif" || ext !=".jpeg")
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Please choose only .jpg, .png and .gif image types!')", true);
}
else
{
FileUpload1.SaveAs(Server.MapPath(imgPath));
Image1.ImageUrl = imgPath;
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);
byte[] theImage = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(theImage, 0, (int)FileUpload1.PostedFile.ContentLength);
int length = theImage.Length;
string fileName = FileUpload1.FileName.ToString();
string type = FileUpload1.PostedFile.ContentType;
int size = FileUpload1.PostedFile.ContentLength;
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
WP_advBLL canwork = new WP_advBLL();
canwork.ExecuteInsert(theImage, type, size, fileName, length, companyID);
Response.Write("File has been uploaded successfully!");
Session["imageID"] = imgName;
}
}
}
我修改了你的代码请看这个
i modified your code please see this
string companyID = this.Label1.Text;
string imgName = FileUpload1.FileName;
string imgPath = "~/Uploads/" + imgName;
int imgSize = FileUpload1.PostedFile.ContentLength;
string ext = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName);
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
if (FileUpload1.PostedFile.ContentLength > 1000000)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);
}
//modified this code
if (ext.ToUpper().Trim() != ".JPG" && ext.ToUpper() != ".PNG" && ext.ToUpper() != ".GIF" && ext.ToUpper() != ".JPEG")
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Please choose only .jpg, .png and .gif image types!')", true);
}
else
{
FileUpload1.SaveAs(Server.MapPath(imgPath));
Image1.ImageUrl = imgPath;
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);
byte[] theImage = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(theImage, 0, (int)FileUpload1.PostedFile.ContentLength);
int length = theImage.Length;
string fileName = FileUpload1.FileName.ToString();
string type = FileUpload1.PostedFile.ContentType;
int size = FileUpload1.PostedFile.ContentLength;
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
{
WP_advBLL canwork = new WP_advBLL();
canwork.ExecuteInsert(theImage, type, size, fileName, length, companyID);
Response.Write("File has been uploaded successfully!");
Session["imageID"] = imgName;
}
}
}
试试这个正则表达式验证器:
Try this Regular Expression Validator:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="fileupload1"
ErrorMessage="Only .jpg,.png,.jpeg,.gif Files are allowed" Font-Bold="True"
Font-Size="Medium"
ValidationExpression="(.*?)\.(jpg|jpeg|png|gif|JPG|JPEG|PNG|GIF)
这篇关于Fileupload图像类型验证C#asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文