/// <summary> /// 上传文件 /// </summary> /// <param name="userID"></param> /// <param name="uploadActionName"></param> /// <param name="filename"></param> /// <param name="searchInfo"></param> /// <param name="customParams"></param> /// <returns></returns> public static TempUploadFile Upload(int userID, string uploadActionName, string filename, string searchInfo, NameValueCollection queryString, ref object customResult, params string[] customParams) { if (userID <= 0) return null; if (searchInfo == null) searchInfo = string.Empty; FileActionBase uploadAction; if (s_UploadActions.TryGetValue(uploadActionName, out uploadAction) == false) return null; uploadAction = uploadAction.CreateInstance(); HttpContext context = HttpContext.Current; TempUploadFile uploadedFile; string tempFileDirectory = Globals.GetPath(SystemDirecotry.Temp_Upload); string tempFilename = string.Concat(DateTimeUtil.Now.ToString("yyyy_MM_dd_"), Guid.NewGuid().ToString("N"), Consts.FileSystem_TempFileExtendName); string tempFilePath = IOUtil.JoinPath(tempFileDirectory, tempFilename); if (!Directory.Exists(tempFileDirectory)) Directory.CreateDirectory(tempFileDirectory); if (uploadAction.BeforeUpload(context, filename, tempFilePath, queryString, ref customResult) == false) return null; string md5; long fileSize; //if (1 == 1) //{ //if (context.Request.Files.Count > 0) //{ //HttpPostedFile postedFile = context.Request.Files[0]; //if (string.IsNullOrEmpty(filename)) // filename = postedFile.FileName; //fileSize = postedFile.ContentLength; //md5 = IOUtil.GetFileMD5Code(postedFile.InputStream); //postedFile.SaveAs(tempFilePath); FileManagerUploadPolicy uploadPolicy = new FileManagerUploadPolicy(tempFilePath); try { using (FileUploader uploader = new FileUploader(uploadPolicy)) { uploader.BeginUpload(); } } catch(System.Security.SecurityException)// ex) { using (Stream stream = uploadPolicy.CreateFileStream(context.Request.Files[0].FileName)) { byte[] buffer = new byte[10240]; int readed = 0; while (readed != buffer.Length) { int l = context.Request.Files[0].InputStream.Read(buffer, readed, buffer.Length); stream.Write(buffer, 0, l); readed += l; } } } if (string.IsNullOrEmpty(filename)) filename = context.Request.Form["filename"]; if (string.IsNullOrEmpty(filename)) filename = uploadPolicy.UploadFileName; fileSize = uploadPolicy.UploadFileSize; md5 = uploadPolicy.UploadFileMD5; if (uploadAction.Uploading(context, filename, tempFilePath, fileSize, fileSize, ref customResult) == false) return null; StringList customParamList = new StringList(); if (customParams != null) { foreach (string customParam in customParams) customParamList.Add(customParam); } uploadedFile = new TempUploadFile(filename, tempFilePath, fileSize, md5); uploadedFile.TempUploadFileID = FileDao.Instance.CreateTempUploadFile(userID, uploadActionName, searchInfo, customParamList, filename, tempFilename, md5, fileSize, uploadedFile.FileID); //} //else // return null; //} //else //{ // //Uploader uploader = new Uploader(); // //uploader.IsSwfUploader = true; // //if (uploader.BeginUpload(tempFilePath, new Uploader.UploadOnProcess(delegate // //{ // // if (uploadAction.Uploading(filename, tempFilePath, uploader.FileSize, uploader.TotalReceived) == false) // // return false; // // return true; // //}))) // //{ // // fileSize = uploader.TotalFileReceived; // // md5 = uploader.MD5Code; // // uploadedFile = uploader; // //} // //else // // return null; //} if (uploadAction.AfterUpload(context, filename, tempFilePath, fileSize, uploadedFile.TempUploadFileID, md5, queryString, ref customResult) == false) return null; return uploadedFile; }
/// <summary> /// 上传文件 /// </summary> /// <param name="userID"></param> /// <param name="uploadActionName"></param> /// <param name="filename"></param> /// <param name="searchInfo"></param> /// <param name="customParams"></param> /// <returns></returns> public static TempUploadFile Upload(int userID, string uploadActionName, string filename, string searchInfo, NameValueCollection queryString, ref object customResult, params string[] customParams) { if (userID <= 0) { return(null); } if (searchInfo == null) { searchInfo = string.Empty; } FileActionBase uploadAction; if (s_UploadActions.TryGetValue(uploadActionName, out uploadAction) == false) { return(null); } uploadAction = uploadAction.CreateInstance(); HttpContext context = HttpContext.Current; TempUploadFile uploadedFile; string tempFileDirectory = Globals.GetPath(SystemDirecotry.Temp_Upload); string tempFilename = string.Concat(DateTimeUtil.Now.ToString("yyyy_MM_dd_"), Guid.NewGuid().ToString("N"), Consts.FileSystem_TempFileExtendName); string tempFilePath = IOUtil.JoinPath(tempFileDirectory, tempFilename); if (!Directory.Exists(tempFileDirectory)) { Directory.CreateDirectory(tempFileDirectory); } if (uploadAction.BeforeUpload(context, filename, tempFilePath, queryString, ref customResult) == false) { return(null); } string md5; long fileSize; //if (1 == 1) //{ //if (context.Request.Files.Count > 0) //{ //HttpPostedFile postedFile = context.Request.Files[0]; //if (string.IsNullOrEmpty(filename)) // filename = postedFile.FileName; //fileSize = postedFile.ContentLength; //md5 = IOUtil.GetFileMD5Code(postedFile.InputStream); //postedFile.SaveAs(tempFilePath); FileManagerUploadPolicy uploadPolicy = new FileManagerUploadPolicy(tempFilePath); try { using (FileUploader uploader = new FileUploader(uploadPolicy)) { uploader.BeginUpload(); } } catch (System.Security.SecurityException)// ex) { using (Stream stream = uploadPolicy.CreateFileStream(context.Request.Files[0].FileName)) { byte[] buffer = new byte[10240]; int readed = 0; while (readed != buffer.Length) { int l = context.Request.Files[0].InputStream.Read(buffer, readed, buffer.Length); stream.Write(buffer, 0, l); readed += l; } } } if (string.IsNullOrEmpty(filename)) { filename = context.Request.Form["filename"]; } if (string.IsNullOrEmpty(filename)) { filename = uploadPolicy.UploadFileName; } fileSize = uploadPolicy.UploadFileSize; md5 = uploadPolicy.UploadFileMD5; if (uploadAction.Uploading(context, filename, tempFilePath, fileSize, fileSize, ref customResult) == false) { return(null); } StringList customParamList = new StringList(); if (customParams != null) { foreach (string customParam in customParams) { customParamList.Add(customParam); } } uploadedFile = new TempUploadFile(filename, tempFilePath, fileSize, md5); uploadedFile.TempUploadFileID = FileDao.Instance.CreateTempUploadFile(userID, uploadActionName, searchInfo, customParamList, filename, tempFilename, md5, fileSize, uploadedFile.FileID); //} //else // return null; //} //else //{ // //Uploader uploader = new Uploader(); // //uploader.IsSwfUploader = true; // //if (uploader.BeginUpload(tempFilePath, new Uploader.UploadOnProcess(delegate // //{ // // if (uploadAction.Uploading(filename, tempFilePath, uploader.FileSize, uploader.TotalReceived) == false) // // return false; // // return true; // //}))) // //{ // // fileSize = uploader.TotalFileReceived; // // md5 = uploader.MD5Code; // // uploadedFile = uploader; // //} // //else // // return null; //} if (uploadAction.AfterUpload(context, filename, tempFilePath, fileSize, uploadedFile.TempUploadFileID, md5, queryString, ref customResult) == false) { return(null); } return(uploadedFile); }