Code
public bool CanUpload(string fileName)
{
if (string.IsNullOrEmpty(fileName))
{
lblmsg.Text = "Please select Files!";
return false;
}
if (fileName.IndexOf("'") > -1)
{
lblmsg.Text = "File name should exclud qotation marks!";
return false;
}
if (fileName.IndexOf(".") == -1)
{
lblmsg.Text = "Unrecognizable file!";
return false;
}
int contentSize = FileUpload.PostedFile.ContentLength;
if (contentSize > 4470784)
{
lblmsg.Text = "File size is larger than 4MB!";
return false;
}
if (contentSize == 0)
{
lblmsg.Text = "File size is 0!";
return false;
}
//附件類型不允許上傳
string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1).ToUpper();
bool canUploadFileType = false;
//允許上傳的類型
string[] canFilesType = new string[] { "JPG", "JPEG", "PNG", "PDF", "PPT", "TXT", "XLS", "DOC" };
for (int i = 0; i < canFilesType.Length; i++)
{
if (canFilesType[i] == fileType)
{
canUploadFileType = true;
}
}
if (canUploadFileType == false)
{
lblmsg.Text = "Unacceptable file!";
return false;
}
return true;
}