[英]How to prevent specific extension in asp.net FileUpload Control?
[英]asp.net FileUpload control restrict files by extension
我在文件上傳后有以下代碼,只能上傳“ PNG”文件。 如何允許同時上傳“ JPG”和“ JPEG”文件?
protected void btnMainPicUPL_Click(object sender, EventArgs e)
{
String ext = System.IO.Path.GetExtension(fulMainPicUPL.FileName);
if (ext == ".png")
{
String path = Server.MapPath("\\~/../Logged_in/AdminFotoUser/UserPics\\");
fulMainPicUPL.SaveAs(path + txtMainPicUPL.Text + ext);
}
else
{
lblServerMSG.ForeColor = System.Drawing.Color.Red;
lblServerMSG.Text = "<br>No hemos podido cargar tu foto!";
}
}
您可以將允許的文件擴展名存儲在數組中,然后對它進行包含操作,如下所示:
string ext = System.IO.Path.GetExtension(fulMainPicUPL.FileName);
string[] allowedExtenstions = new string[] { ".png", ".jpg", ".jpeg" };
if (allowedExtenstions.Contains(ext))
{
string path = Server.MapPath("\\~/../Logged_in/AdminFotoUser/UserPics\\");
fulMainPicUPL.SaveAs(path + txtMainPicUPL.Text + ext);
}
請注意,最好將允許的文件擴展名存儲在appSettings
中的可配置位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.