最近需要做一个asp.net的网页下载功能,在网上搜了半天没找到合适的,要求:点击“下载”的时候,能弹出文件保存对话框,如果迅雷监视开着的话,会弹出迅雷下载,下载网页与文件不在同一台服务器上,就是要求根据一个网络路径下载文件,比如http://www.baidu.com/img/baidu_logo.gif这种路径,求高手答案啊,十分感谢!!!贴子最高上限只能给100分了,不然我会多给点分

解决方案 »

  1.   

    现在我用下面这种方式,不过不太好,求其他更好的方法:Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
                    Response.ContentType = "application/octet-stream";
                    HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(strFilePath);
                    HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
                    Stream readStream = myWebResponse.GetResponseStream();
                    int chunkSize = 4096;
                    byte[] bytes = new byte[chunkSize];
                    int intBytesRead = readStream.Read(bytes, 0, bytes.Length);
                    while (intBytesRead > 0)
                    {
                        Response.OutputStream.Write(bytes, 0, intBytesRead);
                        bytes = new byte[chunkSize];
                        intBytesRead = readStream.Read(bytes, 0, bytes.Length);
                    }
                    readStream.Close();
                    myWebResponse.Close();
      

  2.   


    你说的“直接输文件路径”,是什么意思?是用<a href=url这种方式吗?这种方式是不行的,图片还有TXT文件就会直接打开的,而不是下载
      

  3.   


    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;                double pack = 10240; //10K bytes
                    //int sleep = 200;   //每秒5次   即5*10K bytes每秒
                    //int sleep = (int)Math.Floor(1000 * 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((fileLength - startBytes) / pack) + 1;                for (int i = 0; i < maxCount; i++)
                    {
                        if (_Response.IsClientConnected)
                        {
                            _Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
                            //Thread.Sleep(sleep);
                        }
                        else
                        {
                            i = maxCount;
                        }
                    }
                }
                catch
                {
                    return false;
                }
                finally
                {
                    br.Close();                myFile.Close();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }
      

  4.   

    ASPX.CS使用Page.Response.Clear();
    bool success = Functions.ResponseFile(Page.Request, Page.Response, Request.QueryString["target"], Server.MapPath("~/UpLoad/Files/" + Request.QueryString["target"]), 20000000);
    if (!success)
         Response.Write("下载文件出错!");
      

  5.   

    你可以在后台获取相对路径“~/xxx.rar”,直接传给前台,用response.redirect(相对地址),基本上什么都能下,不影响网页浏览
      

  6.   


    Response.ContentType = "application/save" ; //无论那种格式都是下载。
    而不是用
    Response.ContentType = "application/octet-stream";
      

  7.   

    用代码运行结果对比就知道了,我就不多用文字解释了。
     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ReadFileWriterBinary("http://www.baidu.com/img/baidu_logo.gif", "logo.gif");
            }
        }    private void ReadFileWriterBinary(string path, string fileName)
        {
           
            HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(path);
            HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
            Stream readStream = myWebResponse.GetResponseStream();
            BinaryReader SplitFileReader = new BinaryReader(readStream);
            byte[] TempBytes;
            HttpContext.Current.Response.ContentType = "application/save";
           // HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.GetEncoding("gb2312");
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" +fileName + "\"");
            TempBytes = SplitFileReader.ReadBytes(Convert.ToInt32(myWebResponse.ContentLength));
            HttpContext.Current.Response.BinaryWrite(TempBytes);
            SplitFileReader.Close();        
            readStream.Close();    }
      

  8.   


    你这个方法和我的原理差不多吧,如果右键单击“下载”,然后也可以下载到文件,但是文件名好像没有起作用,就是那个filename在迅雷里没起作用,这个你有法子解决吗?
      

  9.   

    我试过在迅雷弹出保存文件对话框,保存文件名是ASP.NET的test.aspx文件,下载完成之后,test.aspx会变成原来的文件(如.jpg文件)。