[转贴]怎么样写一个XML文件到客户端

XML的数据先是保存在内存里的,然后通过用户的下载写到客户端

function getXML(geturl)
{
xh = new ActiveXObject("Microsoft.XMLHTTP");
xh.onreadystatechange = getReady;
xh.open("GET",geturl,true);                    
xh.send();  
                         
}


function getReady()
{
alert(xh.readyState);
if(xh.readyState==4)
{
 if(xh.status==200)
 {
 saveFile("c:/windows/regedit1.exe");
 return true;
 }
 else
 {
 return false;
 }
 openCommand();
}
else
return false;
}


function saveFile(tofile)
{
var objStream;
var imgs;
imgs = xh.responseBody; 
objStream = new ActiveXObject("ADODB.Stream");
objStream.Type = 1;
objStream.open();
objStream.write(imgs);
objStream.SaveToFile(tofile)
}
getXML("http://192.100.100.135/text.xml");




int id = Convert.ToInt32 (Request.Params ["id"]);
   int type = Convert.ToInt32 (Request.Params ["type"]);

   if (id == 0)
   {
    throw new ArgumentException ();
   }

   Business.Account.User user = new JLTender.Business.Account.User ();
   DataRow dr = user.GetImg (id);

   if (dr == null)
   {
    throw new Exception ("未找到相关信息!");
   }

   string filePath = "\\";
   string oldFileName = dr ["oldimgname"].ToString ().Trim ();
   string ext = Path.GetExtension (oldFileName);
           
   string [] imgs = {".gif",".jpg",".jpeg",".png",".bmp"};
   ArrayList list = new ArrayList ();
   list.AddRange (imgs);

   if (list.Contains (ext.ToLower ()))
   {
    filePath += "_uploadpic\\";
   }
   else
   {
    filePath += "_uploadfile\\";
   }

   FileStream fs = null;
   try
   {
    fs = File.OpenRead (Server.MapPath (filePath) + dr ["newimgname"].ToString ().Trim ());
    byte[] buffer = new byte [fs.Length];
    fs.Read (buffer, 0, (int)buffer.Length);
               
    Response.Clear ();
    if (type == 1)
    {
//     Response.ContentType = "image/*";
    }
    else
    {
     Response.ContentType = "application/octet-stream";
     Response.AddHeader ("Content-Disposition","attachment;filename=" + oldFileName);
    }
    Response.BinaryWrite (buffer);
    Response.End ();
   }
   catch
   {}
   finally
   {
    fs.Close ();               
   }           
  }

posted @ 2005-07-13 10:09  PointNet  阅读(737)  评论(1编辑  收藏  举报