尝试上传excel文件。该代码在chrome& amp; Firefox浏览器。它在IE8中引发了上述错误。我该如何解决这个问题。
private const string ExcelUploadPath = "~/UploadedFiles/";
private void SomeFunction()
{
string dirPath = Server.MapPath(ExcelUploadPath);
string ErrorMsg = SaveUploadedFile(fupCtrl, dirPath)
}
private string SaveUploadedFile(FileUpload fupCtrl, string dirPath)
{
try
{
string sFileName = "";
Random ranObj = null;
int nRandomNum = 0;
ranObj = new Random();
nRandomNum = ranObj.Next();
sFileName = fupCtrl.PostedFile.FileName;
sFileName = sFileName.Substring(0, sFileName.LastIndexOf(".") - 1);
sFileName = sFileName + "_" + nRandomNum.ToString();
sFileName = sFileName +
fupCtrl.FileName.Substring(fupCtrl.FileName.LastIndexOf("."));
fupCtrl.SaveAs(dirPath + sFileName); //exception here
return sFileName;
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
答案 0 :(得分:3)
FileUpload.PostedFile.FileName返回客户端上的完整文件名(包括路径)
Path class提供了许多方法来处理表示路径的字符串。
sFileName = Path.GetFileNameWithoutExtension(fupCtrl.PostedFile.FileName);
sFileName += "_" + nRandomNum.ToString();
sFileName += Path.GetExtension(fupCtrl.FileName);
fupCtrl.SaveAs(Path.Combine(dirPath, sFileName));
答案 1 :(得分:1)
试试这个,它可以起作用。
private const string ExcelUploadPath = "~//UploadedFiles//";
答案 2 :(得分:-1)
HttpPostedFile postedFile = context.Request.Files[FileObject];
string filename = Path.GetFileNameWithoutExtension(postedFile.FileName);