2010-07-12 12 views
5

Actualmente estoy intentando escribir datos (máquina cliente) en un archivo xml donde el usuario puede guardar. Sin embargo, quiero que los usuarios puedan decidir dónde quieren guardar este archivo xml escrito. ¿Hay algún control o código que pueda usar para permitir que los usuarios guarden el archivo?¿Cómo creo un archivo XML con ASP.NET y solicito su descarga?

Actualización:

¿esa es la manera correcta de hacerlo?

**HttpContext.Current.Response.Write(xw.ToStroing()); <<< ??????** 
HttpContext.Current.Response.End(); 

actualización:

XmlWriterSettings settings = new XmlWriterSettings(); 
      settings.Indent = true; 
      MemoryStream ms = new MemoryStream(); 

HttpContext.Current.Response.Clear(); 
      HttpContext.Current.Response.ContentType = "text/xml"; 
      HttpContext.Current.Response.AddHeader("Content-Disposition:", "attachment;filename=" + HttpUtility.UrlEncode(fileName));   

      using (StringWriter sw = new StringWriter()) 
      { 
       using (XmlWriter xw = XmlWriter.Create(ms, settings)) 

       { 
        xw.WriteStartDocument(); 

        xw.WriteStartElement("Name"); 
        xw.WriteStartElement("Application"); 
        ................ 
        ...................... 
        HttpContext.Current.Response.Write(xw.ToStroing()); 
         HttpContext.Current.Response.End(); 

Respuesta

1

aquí es el link mí :) ayuda

1

Cuando se inicia un archivo de descarga, el navegador del cliente manejará el archivo de ubicación de almacenamiento.

Si desea forzar el archivo de descarga en vez de abierto, añadir esta cabecera HTTP para su respuesta:

Content-Disposition: attachment; filename="myFile.xml" 
+0

estoy usando y tengo que actualizar mi pregunta, por favor ver y todavía no funcionan –

+1

probar este lugar: 'usando (XmlWriter xw = XmlWriter.Create (Response.OutputStream))' y luego no hacer el ' HttpContext.Current.Response.Write() ' –

+0

también, no estoy seguro si ha notado que' xw.ToStroing() 'está mal escrito –

0

Cuando se está utilizando el MemoryStream ? Usted debe pasar el StringWriter a la XmlTextWriter constructor luego Response.Write() el contenido de la StringWriter

0

Añadir este después de la línea Response.AddHeader:

Response.ContentType = "application/octet-stream"; 

Esto descarga de archivos, sin importar el tipo de archivo forzar.

Cuestiones relacionadas