This is my Upload Button Code. I want to upload pdf file and then read all the files that is uploaded.
protected void Upload_Files(object sender, EventArgs e)
{
if (fileUpload.HasFile) // CHECK IF ANY FILE HAS BEEN SELECTED.
{
int iUploadedCnt = 0;
int iFailedCnt = 0;
HttpFileCollection hfc = Request.Files;
lblFileList.Text = "Select <b>" + hfc.Count + "</b> file(s)";
if (hfc.Count <= 10) // 10 FILES RESTRICTION.
{
for (int i = 0; i <= hfc.Count - 1; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
if (!File.Exists(Server.MapPath("CopyFiles\\") +Path.GetFileName(hpf.FileName)))
{
DirectoryInfo objDir = new DirectoryInfo(Server.MapPath("CopyFiles\\"));
string sFileName = Path.GetFileName(hpf.FileName);
string sFileExt = Path.GetExtension(hpf.FileName);
// CHECK FOR DUPLICATE FILES.
FileInfo[] objFI = objDir.GetFiles(sFileName.Replace(sFileExt, "") + "*.pdf");
if (objFI.Length > 0)
{
// CHECK IF FILE WITH THE SAME NAME EXISTS
foreach (FileInfo file in objFI)
{
string sFileName1 = objFI[0].Name;
string sFileExt1 = Path.GetExtension(objFI[0].Name);
if (sFileName1.Replace(sFileExt1, "") == sFileName.Replace(sFileExt, ""))
{
iFailedCnt += 1; // NOT ALLOWING DUPLICATE.
break;
}
}
}
else
{
// SAVE THE FILE IN A FOLDER.
hpf.SaveAs(Server.MapPath("CopyFiles\\") + Path.GetFileName(hpf.FileName));
iUploadedCnt += 1;
}
}
}
}
lblUploadStatus.Text = "<b>" + iUploadedCnt + "</b> file(s) Uploaded.";
lblFailedStatus.Text = "<b>" + iFailedCnt + "</b> duplicate file(s) could not be uploaded.";
}
else lblUploadStatus.Text = "Max. 10 files allowed.";
}
else lblUploadStatus.Text = "No files selected.";
}
当我使用iTextSharp.text.pdf.parser使用库时,我在此方法中遇到Path.GetExtension(hpf.FileName )错误;在我的代码中。我正在使用Path.GetExtension(hpf.FileName)从浏览器上传文件。
if (!File.Exists(Server.MapPath("CopyFiles\\") +Path.GetFileName(hpf.FileName)))
{
DirectoryInfo objDir = new DirectoryInfo(Server.MapPath("CopyFiles\\"));
string sFileName = Path.GetFileName(hpf.FileName);
string sFileExt = Path.GetExtension(hpf.FileName);
// CHECK FOR DUPLICATE FILES.
FileInfo[] objFI = objDir.GetFiles(sFileName.Replace(sFileExt, "") + "*.pdf");
if (objFI.Length > 0)
{
// CHECK IF FILE WITH THE SAME NAME EXISTS
foreach (FileInfo file in objFI)
{
string sFileName1 = objFI[0].Name;
string sFileExt1 = Path.GetExtension(objFI[0].Name);
if (sFileName1.Replace(sFileExt1, "") == sFileName.Replace(sFileExt, ""))
{
iFailedCnt += 1; // NOT ALLOWING DUPLICATE.
break;
}
}
}
else
{
// SAVE THE FILE IN A FOLDER.
hpf.SaveAs(Server.MapPath("CopyFiles\\") + Path.GetFileName(hpf.FileName));
iUploadedCnt += 1;
}
}