从控制器获取kmz文件

时间:2011-03-24 13:58:54

标签: asp.net asp.net-mvc asp.net-mvc-2 asp.net-mvc-3

我正在尝试使用一些代码taken here

在给定GET请求的情况下响应kmz文件。这是我的代码:

public void GetKMZ()
{
    this.Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=GoogleMap.kmz");
        this.Response.ContentType = "application/vnd.google-earth.kmz";

        this.Response.AppendHeader("Content-Encoding", "kmz");

    byte[] bytes = null;
    MemoryStream memStream = new MemoryStream();
    XmlTextWriter xmlTW = new XmlTextWriter(memStream,Encoding.UTF8);

    xmlTW.Formatting = Formatting.Indented;
    xmlTW.WriteStartDocument();

    xmlTW.WriteStartElement("kml");
    xmlTW.WriteAttributeString("xmlns", "http://www.opengis.net/kml/2.2");
    xmlTW.WriteStartElement("Document");
        xmlTW.WriteStartElement("Style");
        xmlTW.WriteAttributeString("id", "s1");
        xmlTW.WriteStartElement("LineStyle");
        xmlTW.WriteElementString("color", "7f0000ff");
        xmlTW.WriteElementString("width", "3");
        xmlTW.WriteEndElement();
        xmlTW.WriteEndElement();

        xmlTW.WriteElementString("name", "Chicago Transit Map");
        xmlTW.WriteElementString("description", "Chicago Transit Authority train lines");

        xmlTW.WriteStartElement("Placemark");
            xmlTW.WriteElementString("styleUrl", "#s1");
            xmlTW.WriteElementString("name", "Chicago Transit Map");
            xmlTW.WriteStartElement("LineString");
                xmlTW.WriteElementString("altitudeMode", "relative");
                xmlTW.WriteElementString("coordinates", "-87.89289951324463,41.97881025520548,0 -87.89184808731079,41.97788506340239,0 -87.89150476455688,41.97762983571196,0");

            xmlTW.WriteEndElement();
            xmlTW.WriteEndElement();

        xmlTW.WriteEndElement();

    xmlTW.WriteEndElement(); //Document

    xmlTW.WriteEndDocument(); // kml
    xmlTW.Close();

    bytes = memStream.ToArray(); // vs .GetBuffer();

    MemoryStream memStream2 = new MemoryStream();
    using (ZipOutputStream gzOs = new ZipOutputStream(memStream2))
    {
    ZipEntry entry = new ZipEntry("GoogleMap.kml");
    gzOs.SetLevel(9);
    gzOs.PutNextEntry(entry);
    gzOs.Write(bytes, 0, bytes.Length);
    gzOs.CloseEntry();
    gzOs.Close();
    }
    this.Response.Clear();
    this.Response.BinaryWrite(memStream2.ToArray());
    this.Response.End();
}

如果我使用winrar将其解压缩为kml文件(即我可以在谷歌地图中打开它),生成的kmz文件可以正常工作。不幸的是,谷歌地球/地图不喜欢GET请求产生的kmz文件。我正在使用ASP.NET MVC 3.可以使用zip dll(编译为.net 2.0):

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;

有问题吗?

感谢。

基督教

2 个答案:

答案 0 :(得分:0)

事情似乎适用于DotNetZip:

// http://stackoverflow.com/questions/4717605/asp-net-vb-kml-generator
[AcceptVerbs(HttpVerbs.Get)]
public void GetKMZ()
{
    Response.AppendHeader("Connection", "close");
    Response.ContentType = "application/vnd.google-earth.kmz";
    Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

    ZipOutputStream outzip = new ZipOutputStream(Response.OutputStream);
    outzip.EnableZip64 = Zip64Option.Never;
    outzip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
    outzip.PutNextEntry("doc.kml");

    XmlTextWriter xmlTW = new XmlTextWriter(outzip,Encoding.UTF8);

    xmlTW.Formatting = Formatting.Indented;
    xmlTW.WriteStartDocument();

    xmlTW.WriteStartElement("kml");
    xmlTW.WriteAttributeString("xmlns", "http://www.opengis.net/kml/2.2");
    xmlTW.WriteStartElement("Document");
        xmlTW.WriteStartElement("Style");
        xmlTW.WriteAttributeString("id", "s1");
        xmlTW.WriteStartElement("LineStyle");
        xmlTW.WriteElementString("color", "7f0000ff");
        xmlTW.WriteElementString("width", "3");
        xmlTW.WriteEndElement();
        xmlTW.WriteEndElement();

        xmlTW.WriteElementString("name", "Chicago Transit Map");
        xmlTW.WriteElementString("description", "Chicago Transit Authority train lines");

        xmlTW.WriteStartElement("Placemark");
            xmlTW.WriteElementString("styleUrl", "#s1");
            xmlTW.WriteElementString("name", "Chicago Transit Map");
            xmlTW.WriteStartElement("LineString");
                xmlTW.WriteElementString("altitudeMode", "relative");
                xmlTW.WriteElementString("coordinates", "-87.89289951324463,41.97881025520548,0 -87.89184808731079,41.97788506340239,0 -87.89150476455688,41.97762983571196,0");

            xmlTW.WriteEndElement();
            xmlTW.WriteEndElement();

        xmlTW.WriteEndElement();

    xmlTW.WriteEndElement(); //Document

    xmlTW.WriteEndDocument(); // kml

    xmlTW.Flush();
    outzip.Close();
} 

答案 1 :(得分:0)

csetzkorn的变化很小,而且更通用(在ashx模式下工作,需要kml作为字符串)

    using Ionic.Zip;

public void KMLtoKMZ(HttpContext context, string kml)
        {




            context.Response.ContentType = "application/vnd.google-earth.kmz";
            context.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

            ZipOutputStream outzip = new ZipOutputStream(context.Response.OutputStream);
            outzip.EnableZip64 = Zip64Option.Never;
            outzip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
            outzip.PutNextEntry("doc.kml");
            StreamWriter sw = new StreamWriter(outzip);
            sw.Write(kml);
            sw.Flush();

            outzip.Flush();
            outzip.Close();



        }