protected static bool OutputTempFile(HttpContext context, int userID, int tempUploadFileID, OutputFileMode mode) { TempUploadFile tempFile = FileManager.GetUserTempUploadFile(userID, tempUploadFileID); if (tempFile == null) { return(false); } int index = tempFile.FileName.LastIndexOf('.'); string fileType = tempFile.FileName.Substring(index, tempFile.FileName.Length - index); return(OutputFile(context, tempFile.PhysicalFilePath, tempFile.FileName, fileType, mode)); }
public static PhysicalFileFromTemp Save(int operatorID, TempUploadFile tempUploadFile) { if (operatorID <= 0) { Context.ThrowError(new NotLoginError()); return(null); } if (tempUploadFile == null) { return(null); } PhysicalFileFromTemp file = FileDao.Instance.SaveFile(operatorID, tempUploadFile.TempUploadFileID); string tempUploadFilePath = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Temp_Upload), file.TempUploadServerFileName); string targetFilePath = IOUtil.JoinPath(Globals.GetPath(SystemDirecotry.Upload_File), file.ServerFilePath); string targetDirectory = Path.GetDirectoryName(targetFilePath); try { if (File.Exists(targetFilePath)) { File.Delete(tempUploadFilePath); } else { if (Directory.Exists(targetDirectory) == false) { Directory.CreateDirectory(targetDirectory); } File.Move(tempUploadFilePath, targetFilePath); } } catch { } return(file); }
/// <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); }
public override bool Downloading(HttpContext context) { if (context.Request.QueryString["mode"] != null) { if (StringUtil.EqualsIgnoreCase(context.Request.QueryString["mode"], "image")) { outputMode = OutputFileMode.Inline; } else { outputMode = OutputFileMode.Attachment; if (StringUtil.EqualsIgnoreCase(context.Request.QueryString["mode"], "media")) { isMedia = true; } } } else { outputMode = OutputFileMode.Attachment; } operatorUser = User.Current; userID = operatorUser.UserID; //UserBO.Instance.GetCurrentUserID(); //====处理3.0的附件============================================ if (context.Request.QueryString["v"] == "3") { ProcessV30(context); return(true); } //=================================================== //预览或编辑器里显示 从网络硬盘里插入的附件 if (context.Request.QueryString["diskfileID"] != null) { ProcessDiskFile(context); return(true); } if (context.Request.QueryString["ID"] != null) { try { attachmentID = int.Parse(context.Request.QueryString["ID"].Trim()); } catch { Context.ThrowError <InvalidParamError>(new InvalidParamError("ID")); return(false); //Bbs3Globals.ShowError("error", "参数错误!", 0); } } else { Context.ThrowError <InvalidParamError>(new InvalidParamError("ID")); return(false); //Bbs3Globals.ShowError("error", "参数错误!", 0); } //附件ID小于0,表示这是一个发帖时使用的临时文件 if (attachmentID < 0) { if (isMedia) { TempUploadFile tempFile = FileManager.GetUserTempUploadFile(userID, 0 - attachmentID); if (tempFile == null) { return(false); } int index = tempFile.FileName.LastIndexOf('.'); string fileType = tempFile.FileName.Substring(index, tempFile.FileName.Length - index); ProcessMedia(context, fileType); } else if (false == OutputTempFile(context, userID, 0 - attachmentID, outputMode)) { ShowErrorMessage(context, "文件不存在,可能长时间没有发表导致被系统清理!", "临时文件不存在.gif"); } } //附件ID大于0,表示这是一个真实的附件 else { attachment = PostBOV5.Instance.GetAttachment(attachmentID, outputMode == OutputFileMode.Attachment); if (attachment == null) { if (isMedia) { return(false); } ShowErrorMessage(context, "该附件不存在,可能被移动或被删除!", "文件不存在.gif"); return(false); } if (isMedia) { ProcessMedia(context, "." + attachment.FileType); } string fileID = attachment.FileID; //处理3.0的 #attach:id# if (context.Request.QueryString["m"] != null) { if (context.Request.QueryString["m"].ToLower() == "i" && MaxLabs.bbsMax.Ubb.PostUbbParserV5.isImage(attachment.FileType)) { outputMode = OutputFileMode.Inline; } } switch (Action(context)) { //case "buy": // ProcessBuy(context,fileID,null); // break; default: ProcessDownload(context, fileID); //context.Response.End(); break; } } return(true); }