要配置一个纯粹保存文件的网站,如果上传的是asa、asp、config等扩展名的文件,都是不能下载的,提示403错误,我已经MIME里设置 .* 为 application/octet-stream了

解决方案 »

  1.   

    可能要把原来扩展名的ISAPI删掉的,不然IIS还是会去解释的
    楼主试试吧,记得备份一下~
      

  2.   

    删除完了以后,asp.net版本会回复的1.1,然后就自动还原了,我留了一个没删,这样就可以,凑合用了,问题解决了。谢谢
      

  3.   

    你可以在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我没想到好办法
      

  4.   

    我也没试过,呵呵,只知道那个是负责解释的,5楼的办法也很好,写文件,这样不用动IIS的配置,省去了很多麻烦
      

  5.   

    还是在前面加一个简单的壳吧。GetFile.ashxcontext.Response.WriteFile(context.Server.MapPath(context.Request.QueryString["path"]));