如何配置IIS6,让他能够下载源文件 要配置一个纯粹保存文件的网站,如果上传的是asa、asp、config等扩展名的文件,都是不能下载的,提示403错误,我已经MIME里设置 .* 为 application/octet-stream了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可能要把原来扩展名的ISAPI删掉的,不然IIS还是会去解释的楼主试试吧,记得备份一下~ 删除完了以后,asp.net版本会回复的1.1,然后就自动还原了,我留了一个没删,这样就可以,凑合用了,问题解决了。谢谢 你可以在Global.asax里处理: protected void Application_BeginRequest(object sender, EventArgs e) { string path = Server.MapPath(Request.Url.PathAndQuery); if (!string.IsNullOrEmpty(path)) { string ext = Path.GetExtension(path); if (ext != ".asa" && ext != ".asp" && ext != ".config") { ResponseFile(Request, Response, Path.GetFileName(path), path, 1024 * 1024); } } } public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed) { try { FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); BinaryReader br = new BinaryReader(myFile); try { _Response.AddHeader("Accept-Ranges", "bytes"); _Response.Buffer = false; long fileLength = myFile.Length; long startBytes = 0; int pack = 10240; //10K bytes //int sleep = 200; //每秒5次 即5*10K bytes每秒 int sleep = (int)Math.Floor(1000D * pack / _speed) + 1; if (_Request.Headers["Range"] != null) { _Response.StatusCode = 206; string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' }); startBytes = Convert.ToInt64(range[1]); } _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString()); if (startBytes != 0) { _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength)); } _Response.AddHeader("Connection", "Keep-Alive"); _Response.ContentType = "application/octet-stream"; _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8)); br.BaseStream.Seek(startBytes, SeekOrigin.Begin); int maxCount = (int)Math.Floor((double)(fileLength - startBytes) / pack) + 1; for (int i = 0; i < maxCount; i++) { if (_Response.IsClientConnected) { _Response.BinaryWrite(br.ReadBytes(pack)); Thread.Sleep(sleep); } else { i = maxCount; } } } catch { return false; } finally { br.Close(); myFile.Close(); } } catch { return false; } return true; }光靠配置IIS我没想到好办法 我也没试过,呵呵,只知道那个是负责解释的,5楼的办法也很好,写文件,这样不用动IIS的配置,省去了很多麻烦 还是在前面加一个简单的壳吧。GetFile.ashxcontext.Response.WriteFile(context.Server.MapPath(context.Request.QueryString["path"])); 完全不能同意partial class 本身就是垃圾的观点 继承中方法表的问题 System.Text.Encoding问题 使用C#模仿制作windows记事本 怎么样让控件获取焦点啊? 关于DataGrid的记录定位 求问关于progressBar不能正常显示进度 关于存储过程的问题!!! 水晶报表的小问题 请教c#有象Delphi的DBchart控件吗? Console程序一闪而过的问题 c# 如何计算百分比
楼主试试吧,记得备份一下~
{
string path = Server.MapPath(Request.Url.PathAndQuery);
if (!string.IsNullOrEmpty(path))
{
string ext = Path.GetExtension(path);
if (ext != ".asa" && ext != ".asp" && ext != ".config")
{
ResponseFile(Request, Response, Path.GetFileName(path), path, 1024 * 1024);
}
}
} public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0; int pack = 10240; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor(1000D * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8)); br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor((double)(fileLength - startBytes) / pack) + 1; for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
光靠配置IIS我没想到好办法