我想用Response.WriteFile();防盗链播放MP3,为什么每次运行都弹出提示打开或者保存,而不能直接播放

gasover 2003-10-17 11:49:04
每次弹出一个文件下载对话框,让我打开,保存,或者取消。
而我在网页上直接打开一个http的mp3地址,都可以直接播放的。

我的代码是这样写的,请师兄指点:

FileInfo file = new System.IO.FileInfo("E:\\Songs\\apengyouzaijia.rm");

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";


Response.WriteFile(file.FullName);

Response.End();

...全文
99 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gasover 2003-10-18
  • 打赏
  • 举报
回复
哈哈,解决了:
FileInfo file = new System.IO.FileInfo("E:\\煤炭网程序\\Songs\\apengyouzaijia.rm");

// clear the current output content from the buffer
Response.Clear();
// add the header that specifies
Response.AddHeader("Content-Disposition", "filename=" + file.Name);
// add the header that specifies the file size, so that the browser
// can show the download progress
Response.AddHeader("Content-Length", file.Length.ToString());
// specify that the response is a stream that cannot be read by the
// client and must be downloaded

string fileName = file.Name.ToLower();
int lastIndex = fileName.LastIndexOf(".");
fileName = fileName.Substring(lastIndex);

// 根据文件后缀指定文件的Mime类型
switch (fileName)
{
case ".rm":
Response.ContentType = "application/vnd.rn-realmedia";
break;
case ".mp3":
Response.ContentType = "audio/mpeg3";
break;
case ".asf":
Response.ContentType = "video/x-ms-asf";
break;
case ".avi":
Response.ContentType = "video/avi";
break;
case ".wav":
Response.ContentType = "audio/wav";
break;
case ".mpg":
Response.ContentType = "video/mpeg";
break;
case "mpeg":
Response.ContentType = "video/mpeg";
break;
default:
Response.ContentType = "application/octet-stream";
break;
}

// send the file stream to the client
Response.WriteFile(file.FullName);
// stop the execution of this page
Response.End();
gasover 2003-10-17
  • 打赏
  • 举报
回复
我用这个方法来参考无url屏蔽mp3的url点击后的HttpWebResponse参数,和我的也没有什么差别。为何它就是播放,而我就是弹出提示窗口呢?
------------------------------------
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.cwestc.com/news/rm/tsyj.rm");
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

string contenttype = myHttpWebResponse.Headers.ToString();
Response.Write(contenttype);
--------------------------------
运行后页面输出:
Server: Microsoft-IIS/5.0 X-Powered-By: ASP.NET Date: Fri, 17 Oct 2003 04:38:25 GMT Content-Type: application/octet-stream Accept-Ranges: bytes Last-Modified: Sat, 08 Feb 2003 03:18:50 GMT ETag: "049b3cc20cfc21:d15" Content-Length: 1135166
bhys 2003-10-17
  • 打赏
  • 举报
回复
有播放器没有
qimini 2003-10-17
  • 打赏
  • 举报
回复
Mark
速马 2003-10-17
  • 打赏
  • 举报
回复
应该和MIME有关吧
gasover 2003-10-17
  • 打赏
  • 举报
回复
up :)
loulanlouzhu 2003-10-17
  • 打赏
  • 举报
回复
xuexi
gasover 2003-10-17
  • 打赏
  • 举报
回复
up
neodotnet 2003-10-17
  • 打赏
  • 举报
回复
帮你UP
gasover 2003-10-17
  • 打赏
  • 举报
回复
rm 播放器也有, 我常常听歌的。
gasover 2003-10-17
  • 打赏
  • 举报
回复
to 碧海银沙:
如果我没有播放器,为什么我在网页上直接打开一个http的mp3地址,都可以直接播放的。

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧