public string UploadFile(string filePath, int maxSize, string[] fileType, string filename, 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.Length - 3, 3); 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 = filename; string strFilePathTemp = strFilePath; strOldName = strFileNameTemp + "." + strNewName; strFilePath = strFilePath + "\\" + strOldName; TargetFile.PostedFile.SaveAs(strFilePath); Result = strOldName; TargetFile.Dispose(); } else { return("TYPE_ERR"); } } else { return("SIZE_ERR"); } return(Result); }
/// <summary> /// 上传文件 /// </summary> /// <param name="filePath">保存文件地址</param> /// <param name="maxSize">文件最大大小</param> /// <param name="fileType">文件后缀类型</param> /// <param name="TargetFile">控件名</param> /// <param name="saveFileName">保存后的文件名和地址</param> /// <param name="fileSize">文件大小</param> /// <returns></returns> public string UploadFile(string filePath, int maxSize, string[] fileType, System.Web.UI.HtmlControls.HtmlInputFile TargetFile, out string saveFileName, out int fileSize) { saveFileName = ""; fileSize = 0; string Result = ""; //bool typeFlag = false; string FilePath = filePath; int MaxSize = maxSize; string strFileName, strNewName, strFilePath; if (TargetFile.PostedFile.FileName == "") { return("请选择上传的文件"); } 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) { string strFileNameTemp = GetUploadFileName(); string strFilePathTemp = strFilePath; strOldName = strFileNameTemp + strNewName; strFilePath = strFilePath + "\\" + strOldName; fileSize = TargetFile.PostedFile.ContentLength / 1024; saveFileName = strFilePath.Substring(strFilePath.IndexOf("FileUpload\\")); TargetFile.PostedFile.SaveAs(strFilePath); TargetFile.Dispose(); } else { return("上传文件超出指定的大小"); } return(Result); }
/// <summary> /// 上传文件 /// </summary> /// <param name="filePath">保存文件地址</param> /// <param name="maxSize">文件最大大小</param> /// <param name="fileType">文件后缀类型</param> /// <param name="filename">指定保存后的文件名</param> /// <param name="TargetFile">控件名</param> /// <returns></returns> public string UploadFile( string filePath, int maxSize, string[] fileType, string filename, 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.Length - 3, 3); 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 = filename; string strFilePathTemp = strFilePath; strOldName = strFileNameTemp + "." + strNewName; strFilePath = strFilePath + "\\" + strOldName; TargetFile.PostedFile.SaveAs(strFilePath); Result = strOldName; TargetFile.Dispose(); } else { return "TYPE_ERR"; } } else { return "SIZE_ERR"; } return (Result); }
/// <summary> /// 上传文件 /// </summary> /// <param name="filePath">保存文件地址</param> /// <param name="maxSize">文件最大大小</param> /// <param name="fileType">文件后缀类型</param> /// <param name="TargetFile">控件名</param> /// <param name="saveFileName">保存后的文件名和地址</param> /// <param name="fileSize">文件大小</param> /// <returns></returns> public string UploadFile( string filePath, int maxSize, string[] fileType, HtmlInputFile TargetFile, out string saveFileName, out int fileSize) { saveFileName = ""; fileSize = 0; string Result = ""; bool typeFlag = false; string FilePath = filePath; int MaxSize = maxSize; string strFileName, strNewName, strFilePath; if (TargetFile.PostedFile.FileName == "") { return "请选择上传的文件"; } 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) { string strFileNameTemp = GetUploadFileName(); string strFilePathTemp = strFilePath; strOldName = strFileNameTemp + strNewName; strFilePath = strFilePath + "\\" + strOldName; fileSize = TargetFile.PostedFile.ContentLength / 1024; saveFileName = strFilePath.Substring(strFilePath.IndexOf("FileUpload\\")); TargetFile.PostedFile.SaveAs(strFilePath); TargetFile.Dispose(); } else { return "上传文件超出指定的大小"; } return (Result); }