我想创建一个视频流网站! 我将视频放置在网站根目录之前的文件夹中,以防止直接访问,并且我找到了一些代码来通过ashx文件将这些文件作为视频标签的源进行流处理。但我想阻止idm下载此流。
public void ProcessRequest(HttpContext上下文) { 尝试 { bool isValid = false; 字符串ID = context.Request.QueryString [“ id”]。ToString(); 如果(id ==“ 1”) { isValid = true; } 其他 { isValid = false; } 如果(isValid) { 字符串mimetype =“ video / mp4”;
string path = HttpContext.Current.Server.MapPath("~");
string[] paths = path.Split('\\');
path = "";
for (int i = 0; i < paths.Length - 2; i++)
{
path += paths[i] + "\\";
}
path += "MAProjectTestVideos\\";
string fullpath = path + "2.mp4";
if (System.IO.File.Exists(fullpath))
{
context.Response.ContentType = mimetype;
if (!String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
{
RangeDownload(fullpath, context);
}
else
{
long fileLength = File.OpenRead(fullpath).Length;
context.Response.AddHeader("Content-Length", fileLength.ToString());
context.Response.TransmitFile(fullpath);
}
}
else
{
//File Not Found
}
}
else
{
}
}
catch (Exception)
{
}
}
private void RangeDownload(string fullpath, HttpContext context)
{
try
{
long size, start, end, length, fp = 0;
using (StreamReader reader = new StreamReader(fullpath))
{
size = reader.BaseStream.Length;
start = 0;
end = size - 1;
length = size;
context.Response.AddHeader("Accept-Ranges", "0-" + size);
if (!String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
{
long anotherStart = start;
long anotherEnd = end;
string[] arr_split = context.Request.ServerVariables["HTTP_RANGE"].Split(new char[] { Convert.ToChar("=") });
string range = arr_split[1];
if (range.IndexOf(",") > -1)
{
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
}
if (range.StartsWith("-"))
{
anotherStart = size - Convert.ToInt64(range.Substring(1));
}
else
{
arr_split = range.Split(new char[] { Convert.ToChar("-") });
anotherStart = Convert.ToInt64(arr_split[0]);
long temp = 0;
anotherEnd = (arr_split.Length > 1 && Int64.TryParse(arr_split[1].ToString(), out temp)) ? Convert.ToInt64(arr_split[1]) : size;
}
anotherEnd = (anotherEnd > end) ? end : anotherEnd;
if (anotherStart > anotherEnd || anotherStart > size - 1 || anotherEnd >= size)
{
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
}
start = anotherStart;
end = anotherEnd;
length = end - start + 1;
fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
context.Response.StatusCode = 206;
}
}
context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
context.Response.AddHeader("Content-Length", length.ToString());
context.Response.TransmitFile(fullpath, fp, length);
context.Response.End();
}
catch (Exception)
{
}
}