【发布时间】:2017-07-28 14:30:46
【问题描述】:
我正在寻找一种简单的方法来检查上传文件的扩展名,如果是 PDF 文件,请执行某些操作,否则会显示一条警告消息(错误的文件类型),但我的代码的问题是如果我选择了任何文件类型而不是 PDF,它将显示带有此消息的错误页面:
Server Error in '/' Application.
PDF header signature not found.
Exception Details: iTextSharp.text.exceptions.InvalidPdfException: PDF header signature not found.
<asp:FileUpload runat="server" ID="file1" AllowMultiple="true" />
string fileName = Path.GetFileName(file1.FileName);
FileInfo fi = new FileInfo(fileName);
string ext = fi.Extension;
if (ext == ".pdf")
{
//do something
}
else
Label1.Text = string.Format("wrong file type");
【问题讨论】:
-
像匿名懦夫在stackoverflow.com/questions/10621936/…所说的那样做