C# 获取发布的文件扩展名 asp.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19333911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get posted file extension asp.net
提问by Anas Naim
I keep getting "Only images are allowed" and I tried "file.PostedFile.FileName" also not working!!
我不断收到“只允许图像”,我试过“file.PostedFile.FileName”也不起作用!!
this code is written in a separate class..
这段代码写在一个单独的类中..
public static String UploadFile(FileUpload file, String type, out String filename)
{
String ext = System.IO.Path.GetExtension(file.FileName);
filename = "";
if (file.PostedFile.ContentLength > 2000000)
{
return "File is larger than 2 MB";
}
else if (type != "File")
{
if (ext.ToLower() != ".jpg" || ext.ToLower() != ".png" || ext.ToLower() != ".gif" || ext.ToLower() != ".jpeg")
{
return "Only images are allowed";
}
else
{
filename = System.IO.Path.GetRandomFileName() + "_" + file.PostedFile.FileName;
String root = HttpContext.Current.Server.MapPath("~/Images/");
file.SaveAs(root + type + "/" + filename);
return "Success";
}
}
else
{
filename = System.IO.Path.GetRandomFileName() + "_" + file.PostedFile.FileName;
String root = HttpContext.Current.Server.MapPath("~/Files/");
file.SaveAs(root + filename);
return "Success";
}
}
采纳答案by NMathur
Your condition is wrong, it should be like following:
你的条件不对,应该是这样的:
if (ext.ToLower() != ".jpg" && ext.ToLower() != ".png" && ext.ToLower() != ".gif" && ext.ToLower() != ".jpeg")
{
return "Only images are allowed";
}
else
{
///statement
}
OR
或者
if (ext.ToLower() == ".jpg" || ext.ToLower() == ".png" || ext.ToLower() == ".gif" || ext.ToLower() == ".jpeg")
{
///statement
}
else
{
return "Only images are allowed";
}
回答by volpav
Your condition for checking for valid extension is logically incorrect (always evaluates to true
). It should be like this (||
are replaced with &&
):
您检查有效扩展名的条件在逻辑上不正确(始终计算为true
)。应该是这样的(||
被替换为&&
):
if (ext.ToLower() != ".jpg" && ext.ToLower() != ".png" && ext.ToLower() != ".gif" && ext.ToLower() != ".jpeg")
{
return "Only images are allowed";
}
回答by Daniel Gimenez
@volpav's answer will fix your problem, but that big if
isn't the cleanest way to handle the problem.
@volpav 的答案将解决您的问题,但那if
不是解决问题的最干净方法。
More elegant would be to define a list of accepted extensions and check to see if ext
is in the list. The advantages to this would be that it is easier to maintain if you ever have to change the valid types later, and that you can make extensions user definable if that is desirable.
更优雅的是定义一个可接受的扩展列表并检查是否ext
在列表中。这样做的好处是,如果您以后必须更改有效类型,则更容易维护,并且如果需要,您可以使扩展可由用户定义。
In the example below I am defining a constant (well readonly variable) for my class that contains an array with all exceptions, and use the Contains()
extension method to test to see if ext
exists within it when validating in UploadFile
在下面的示例中,我为我的类定义了一个常量(以及只读变量),该类包含一个包含所有异常的数组,并使用Contains()
扩展方法来测试ext
在验证时是否存在于其中UploadFile
public static readonly string[] VALID_EXTENSIONS =
new string[4] { ".png", ".jpg", ".gif", ".jpeg" };
// in UploadFile ...
if (!VALID_EXTENSIONS.Contains(ext.ToLower())) {
return "Only images are allowed";
}
By making it static in the above code, I could use this list in the UI to indicate what are excepted extensions, instead of having the user guess what is a valid image type (There are, after all, other image types than those you have included).
通过在上面的代码中将其设为静态,我可以在 UI 中使用此列表来指示哪些是例外扩展名,而不是让用户猜测什么是有效的图像类型(毕竟,除了您拥有的图像类型之外,还有其他图像类型包括)。