要求根据url参数访问数据库并下载数据库内的数据
 public partial class Terrain : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            String fp=@"D:\yang";
            Directory.SetCurrentDirectory(fp);
            int level = int.Parse(Request.QueryString["level"]);
            int row = int.Parse(Request.QueryString["row"]);
            int col = int.Parse(Request.QueryString["col"]);
            GetTerrainTile(level, row, col);
        }
        private void GetTerrainTile(int level, int row, int col)
        {
            try
            {   
                String cnnStr = ConfigurationManager.ConnectionStrings["GE"].ConnectionString;
                using (SqlConnection cnn = new SqlConnection(cnnStr))
                {
                    String cmdStr = @"declare @s varchar(50) 
                                      select @s=DataBaseName from Metadata_DB where ID=(select DB from Metadata_Terrain where Level='" + level + "' and Row='" + row + "' and Col='" + col + "') exec('use '+@s+' select Data from Data where Level=''" + level + "'' and Row=''" + row + "'' and Col=''" + col + "''')"; 
                    SqlCommand cmd = new SqlCommand(cmdStr, cnn);
                    cnn.Open();
                    SqlDataReader dataReader = cmd.ExecuteReader();
                    while (dataReader.Read())
                    {
                        Response.BinaryWrite((byte[])dataReader["Data"]);
                                            }
                    //FileStream fs = new FileStream("Terrain1.bil", FileMode.Create);
                    //fs.Write((byte[])dataReader["Data"], 0, 10000);
                    //ZipOutputStream zs = new ZipOutputStream(fs);
                    //zs.SetLevel(9);                    //fs.Close();
                    cnn.Close();
                }
                Response.ClearContent();
                Response.ContentType = "application/zip";
                Response.AppendHeader("Content-Length", "application/x-msdownload");
                Response.AppendHeader("")
            }
            catch (SqlException e)
            {
                Response.Write(e.Message.ToString());
            }
            Response.End();
            Response.Close();
        }
    }下面是xml文件
<?xml version="1.0"?>
<configuration>
    <appSettings>
    </appSettings>
    <connectionStrings>
      <add name="GE" connectionString="Data Source=YAOGAN-SERVER;Initial Catalog=VRMSMain;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
    </connectionStrings>
<system.web>   
</system.web>
</configuration>
错误说XML 文档必须有一个顶层元素。处理资源 'http://localhost:4519/Terrain/Terrain.aspx?level=0&row=3696&col=8429' 时出错。 
代码是网上找的加自己改的,主要是不明白红字部分,大家帮忙解答下

解决方案 »

  1.   

    //输出数据到浏览器 
    Response.ContentType = "application/zip";//类型
    Response.AppendHeader("Content-Length", "application/x-msdownload");//将 HTTP 头添加到输出流。 
    Response.AppendHeader("")
    文件下载,指定默认名 
    Response.AddHeader(”content-type”,”application/x-msdownload”); 
    Response.AddHeader(”Content-Disposition”,”attachment;filename=要下载的文件名.rar”); 
      

  2.   

    调试跟踪看看数据是否存在,XML是否有空格
      

  3.   

    2楼正确。Response.ContentType = “附件类型”
    Response.AddHeader("Content-Disposition", "attachment;filename=" & HttpUtility.UrlEncode(“附件名称”))