protected static bool OutputFileByID(HttpContext context, string fileID, string fileName, string fileType, OutputFileMode mode) { PhysicalFile file = FileManager.GetFile(fileID); if (file == null) return false; return OutputFile(context, file.PhysicalFilePath, fileName, fileType, mode); }
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 override bool Downloading(System.Web.HttpContext context) { string id = context.Request.QueryString["id"]; int photoID = -1; if (StringUtil.TryParse(id, out photoID)) { Photo photo = AlbumBO.Instance.GetPhoto(photoID); if (photo != null) { //using (ErrorScope es = new ErrorScope()) //{ bool canVisit = AlbumBO.Instance.CanVisitAlbum(User.Current, photo.Album); if (canVisit == false) { return(false); } //} string fileName = photo.Name; string contentType = string.Empty; if (StringUtil.EndsWithIgnoreCase(photo.Name, photo.FileType) == false) { fileName += photo.FileType; } OutputFileMode outputFileMode = OutputFileMode.Inline; string mode = context.Request.QueryString["mode"]; if (mode == "download") { outputFileMode = OutputFileMode.Attachment; } OutputFileByID(context, photo.FileID, fileName, photo.FileType, outputFileMode); return(true); } } return(false); }
protected static bool OutputFile(HttpContext context, string filePath, string fileName, string fileType, OutputFileMode mode) { if (context.Items.Contains("need-compress")) { context.Items.Remove("need-compress"); } string contentType = GetContentType(fileType); FileInfo fileInfo = new FileInfo(filePath); #region 如果包含If-Modified-Since信息,检查文件是否从未修改,如果是,直接输出304头,无需重复下载 try { if (context.Request.Headers["If-Modified-Since"] != null)// && DateTime.Parse(context.Request.Headers["If-Modified-Since"].Split(';')[0]) > lastModified.AddSeconds(-1)) { DateTime ifModifiedSince; if (DateTime.TryParse(context.Request.Headers["If-Modified-Since"].Split(';')[0], out ifModifiedSince)) { DateTime lastModified = fileInfo.LastWriteTime; //File.GetLastWriteTime(filePath); if (ifModifiedSince > lastModified.AddSeconds(-1)) { context.Response.StatusCode = 304; context.Response.StatusDescription = "Not Modified"; return(false); } } } } catch { } #endregion #region 输出HTTP头信息 context.Response.Clear(); context.Response.ClearHeaders(); context.Response.Buffer = false; context.Response.ContentType = contentType; if (context.Request.Headers["Accept-Charset"] == null) { fileName = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(fileName)).Replace("+", "%20"); } if (mode == OutputFileMode.Attachment) { context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); } else { context.Response.AppendHeader("Content-Disposition", "inline;filename=" + fileName); } #endregion //context.Response.AppendHeader("Content-Length", fileInfo.Length.ToString()); //context.Response.WriteFile(filePath); if (fileInfo.Exists) { context.Response.TransmitFile(filePath); } else { context.Response.TransmitFile(Globals.GetPath(SystemDirecotry.Assets_Images, "notfound.gif")); } // 结束文件下载 context.Response.End(); return(true); }
protected static bool OutputFileByID(HttpContext context, string fileID, string fileName, string fileType, OutputFileMode mode) { PhysicalFile file = FileManager.GetFile(fileID); if (file == null) { return(false); } return(OutputFile(context, file.PhysicalFilePath, fileName, fileType, mode)); }
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 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; }
protected static bool OutputFile(HttpContext context, string filePath, string fileName, string fileType, OutputFileMode mode) { if (context.Items.Contains("need-compress")) context.Items.Remove("need-compress"); string contentType = GetContentType(fileType); FileInfo fileInfo = new FileInfo(filePath); #region 如果包含If-Modified-Since信息,检查文件是否从未修改,如果是,直接输出304头,无需重复下载 try { if (context.Request.Headers["If-Modified-Since"] != null)// && DateTime.Parse(context.Request.Headers["If-Modified-Since"].Split(';')[0]) > lastModified.AddSeconds(-1)) { DateTime ifModifiedSince; if (DateTime.TryParse(context.Request.Headers["If-Modified-Since"].Split(';')[0], out ifModifiedSince)) { DateTime lastModified = fileInfo.LastWriteTime; //File.GetLastWriteTime(filePath); if (ifModifiedSince > lastModified.AddSeconds(-1)) { context.Response.StatusCode = 304; context.Response.StatusDescription = "Not Modified"; return false; } } } } catch { } #endregion #region 输出HTTP头信息 context.Response.Clear(); context.Response.ClearHeaders(); context.Response.Buffer = false; context.Response.ContentType = contentType; if (context.Request.Headers["Accept-Charset"] == null) fileName = HttpUtility.UrlEncode(Encoding.UTF8.GetBytes(fileName)).Replace("+", "%20"); if (mode == OutputFileMode.Attachment) { context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName); } else { context.Response.AppendHeader("Content-Disposition", "inline;filename=" + fileName); } #endregion //context.Response.AppendHeader("Content-Length", fileInfo.Length.ToString()); //context.Response.WriteFile(filePath); if (fileInfo.Exists) context.Response.TransmitFile(filePath); else context.Response.TransmitFile(Globals.GetPath(SystemDirecotry.Assets_Images, "notfound.gif")); // 结束文件下载 context.Response.End(); return true; }
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); }