我在asp.net中写了一段下载文件的代码:
string path ="C:\\test.txt";
System.IO.FileInfo file = new System.IO.FileInfo(path); 
Response.Clear(); 
Page.Response.AddHeader( "Content-Type", "MIME类型" );
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));  Response.AddHeader("Content-Length", file.Length.ToString()); 
Response.ContentType = "application/octet-stream"; 
Response.WriteFile(file.FullName); 
Response.End();
编译通过,在2000系统中登陆页面可以成功下载test.txt文件,在XP系统中登陆页面下载的却是aspx文件而不是指定的test.txt文件
 我的开发环境是2000server+.net2003,IIS在2000server(开发机子)上面。
请问高手怎么解决

解决方案 »

  1.   

    if(!System.IO.File.Exists(strFile))
    {
    Response.Write("<script language='javascript'>alert('对不起,文件不存在!');</script>");
    return;
    }
    Response.Clear();
    Response.ClearHeaders();
    Response.Charset = "GB2312";
    Response.ContentEncoding =System.Text.Encoding.UTF8;
    Response.ContentType = "application/octet-stream"; 
    FileInfo fi=new FileInfo(strFile);
    Response.AddHeader("Content-Disposition","attachment;  filename="  +  HttpUtility.UrlEncode(fi.Name)) ;
    Response.AddHeader("Content-Length",fi.Length.ToString());
    byte[] tmpbyte=new byte[1024*8];
    FileStream fs=fi.OpenRead();
    int count;
    while((count=fs.Read(tmpbyte,0,tmpbyte.Length))>0)
    {
    Response.BinaryWrite(tmpbyte);
    Response.Flush();
    }
    fs.Close();
    Response.End();
    用这个试一下