asp.NET FileUpload控件获取不到完整路径
我常用的上传方法:
/// <summary>
/// 上传文件
/// </summary>
/// <param name="filePath">文件保存路径</param>
/// <param name="filename">保存文件夹名称</param>
/// <param name="maxSize">最大长度</param>
/// <param name="fileType">文件类型</param>
/// <param name="TargetFile">上传控件</param>
/// <returns>返回虚拟路径</returns>
public string UploadFile(string filePath,string filename, int maxSize, string[] fileType, System.Web.UI.HtmlControls.HtmlInputFile TargetFile)
{
string Result = "UnDefine";
bool typeFlag = false;
string FilePath = filePath;
int MaxSize = maxSize;
string strFileName, strNewName, strFilePath;
if (TargetFile.PostedFile.FileName == "")
{
return "FILE_ERR";
}
strFileName = TargetFile.PostedFile.FileName;
TargetFile.Accept = "*/*";
strFilePath = FilePath;
if (Directory.Exists(strFilePath) == false)
{
Directory.CreateDirectory(strFilePath);
}
FileInfo myInfo = new FileInfo(strFileName);
string strOldName = myInfo.Name;
strNewName = strOldName.Substring(strOldName.LastIndexOf("."));
strNewName = strNewName.ToLower();
if (TargetFile.PostedFile.ContentLength <= MaxSize)
{
for (int i = 0; i <= fileType.GetUpperBound(0); i++)
{
if (strNewName.ToLower() == fileType[i].ToString()) { typeFlag = true; break; }
}
if (typeFlag)
{
string strFileNameTemp = GetUploadFileName();
string strFilePathTemp = strFilePath;
float strFileSize = TargetFile.PostedFile.ContentLength;
strOldName = strFileNameTemp + strNewName;
strFilePath = strFilePath + "\\" + strOldName;
TargetFile.PostedFile.SaveAs(strFilePath);
Result = filename + "/" + strOldName;//返回虚拟路径
TargetFile.Dispose();
}
else
{
return "TYPE_ERR";
}
}
else
{
return "SIZE_ERR";
}
return (Result);
}