📄 downloadpostattachment.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System.Web;using System.Web.UI;using CommunityServer.Components;
using CommunityServer.Discussions.Components;
namespace CommunityServer.Discussions.Controls
{
/// <summary>
/// Summary description for PostAttachment.
/// </summary>
public class DownloadPostAttachment : Control
{
CSContext csContext = CSContext.Current;
protected override void Render(HtmlTextWriter writer)
{
// Get the forum the attachment is in
//
Forum forum = Forums.GetForumByPostID(csContext.PostID, Users.GetUser().UserID, false);
if (forum == null)
{
throw new CSException(CSExceptionType.SectionNotFound, csContext.ForumID.ToString());
}
// Get the ForumPermission
//
ForumPermission permission = forum.ResolvePermission( Users.GetUser() ) as ForumPermission;
// if user is not allowed to read this post, return
//
if (!permission.Read )
{
throw new CSException(CSExceptionType.GeneralAccessDenied, csContext.ForumID.ToString());
}
CommunityServer.Components.PostAttachment attachment = Posts.GetAttachment(csContext.PostID);
// Todo: make the context display in browser for browser-friendly items (images, videos, etc)
//
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ContentType = attachment.ContentType;
if (!ValidateContentTypesDisplayInBrowser(attachment.ContentType))
System.Web.HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=\"" + HttpUtility.UrlPathEncode(attachment.FileName) + "\"");
if(attachment.RealFileName != "")
{
string realFileName = CommunityServer.Components.PostAttachment.GetAttachmentPath(attachment.UserID, attachment.RealFileName);
// 以下代码为附件多线程/断点续传下载支持,由bestcomy完成,宝玉 修改了其中的bug
#region 2005-1-11,modified by bestcomy from http://www.cnblogs.com/bestcomy
System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
System.Web.HttpRequest Request = System.Web.HttpContext.Current.Request;
System.IO.FileStream iStream = new System.IO.FileStream(realFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read,System.IO.FileShare.Read);
// Buffer to read 10K bytes in chunk:
byte[] buffer = new byte[10240];
// Length of the file:
int length;
// Total bytes to read:
long dataToRead = iStream.Length;
long p = 0;
if(Request.Headers["Range"]!=null)
{
Response.StatusCode = 206;
string range = Request.Headers["Range"];
range = range.Replace("bytes=","");
range = range.Substring(0, range.IndexOf("-"));
p = long.Parse( range );
}
if(p != 0)
{
Response.AddHeader("Content-Range","bytes " + p.ToString() + "-" + ((long)(dataToRead - 1)).ToString() + "/" + dataToRead.ToString());
}
Response.AddHeader("Content-Length",((long)(dataToRead-p)).ToString());
iStream.Position = p;
dataToRead = dataToRead - p;
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10240);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
buffer= new byte[10240];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
iStream.Close();
#endregion
}
else
{
System.Web.HttpContext.Current.Response.OutputStream.Write(attachment.Content, 0, attachment.Length);
}
System.Web.HttpContext.Current.Response.End();
}
/// <summary>
/// 检查是否直接浏览器打开(不弹出保存对话框)
/// </summary>
/// <param name="rawContentType"></param>
/// <returns></returns>
bool ValidateContentTypesDisplayInBrowser(string rawContentType)
{
string[] ContentTypes = SiteSettingsManager.GetSiteSettings().ContentTypesDisplayInBrowser.Split(';');
string[] rawTypes = rawContentType.ToLower().Split('/');
foreach (string contentType in ContentTypes)
{
string[] types = contentType.ToLower().Split('/');
if (types.Length != 2)
continue;
if ((types[0] == rawTypes[0]) && (types[1] == "*" || types[1] == rawTypes[1]))
{
return true;
}
}
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -