一網友需要對所寫的代碼進行重構,原代碼如下:
/// <summary>
/// 檢查上傳檔案類型
/// </summary>
/// <param name="type">檔案類型</param>
/// <returns></returns>
public string strType(string type)
{
type = type.ToLower();
type = type.Substring(type.LastIndexOf("."));
if ((".swf,.flv,.doc,.docx,.jpg,.xls,.xlsx,.txt,.ppt,.pptx,.pub").Contains(type))
{
return type;
}
else
switch (type)
{
//case ".txt"://txt文本檔案
// type = ".rtf";
// break;
case ".wps"://wps檔案
type = ".doc";
break;
case ".et"://wps的表格檔案
type = ".xls";
case ".pps":
case ".dps":
type = ".ppt";
}
return type;
}
}
public string strType(string type)
{
Dictionary<string, string> dict_type = new Dictionary<string, string>();
dict_type.Add(".swf", "swf");
dict_type.Add(".flv", "flv");
dict_type.Add(".doc", "doc");
dict_type.Add(".docx", "docx");
dict_type.Add(".jpg", "jpg");
dict_type.Add(".xls", "xls");
dict_type.Add(".xlsx", "xlsx");
dict_type.Add(".txt", "txt");
dict_type.Add(".ppt", "ppt");
dict_type.Add(".pptx", "pptx");
dict_type.Add(".pub", "pub");
dict_type.Add(".wps", "doc");
dict_type.Add(".et", "xls");
dict_type.Add(".pps", "ppt");
dict_type.Add(".dps", "ppt");
//如果以後還在有類型需要判斷,請加在這裡。
if (dict_type.ContainsKey(type))
return dict_type[type].ToString();
return string.Empty;
}