我常用的上传方法:
/// <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 = 0i <= fileType.GetUpperBound(0)i++)
{
if (strNewName.ToLower() == fileType[i].ToString()) { typeFlag = truebreak}
}
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)
}
虚拟路径设置到你Web.Config所在的那个文件夹比如你Web.Config所在的文件夹在E:\webset1\Web.Config
那么你的虚拟路径应该是E:\webset1
IIS6.0里面会把你设置的这个路径文件夹做为根目录,当访问这个站点的时候会调用里面的默认首页Default.aspx(默认情况下)
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)