Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 从控制器获取kmz文件_Asp.net_Asp.net Mvc_Asp.net Mvc 2_Asp.net Mvc 3 - Fatal编程技术网

Asp.net 从控制器获取kmz文件

Asp.net 从控制器获取kmz文件,asp.net,asp.net-mvc,asp.net-mvc-2,asp.net-mvc-3,Asp.net,Asp.net Mvc,Asp.net Mvc 2,Asp.net Mvc 3,我正在尝试使用一些代码: 使用给定GET请求的kmz文件进行响应。这是我的代码: public void GetKMZ() { this.Response.Clear(); Response.AddHeader("Content-Disposition", "attachment;filename=GoogleMap.kmz"); this.Response.ContentType = "application/vnd.google-earth.kmz";

我正在尝试使用一些代码:

使用给定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将生成的kmz文件解压缩为kml文件,也就是说,我可以在谷歌地图中打开它,那么生成的kmz文件就可以工作。不幸的是,googleearth/map不喜欢GET请求生成的kmz文件。我正在使用ASP.NETMVC3。无法为.net 2.0编译zip dll:

using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Checksums;
有问题吗

谢谢


Christian

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();
} 

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();



        }

根据维基百科的说法,你需要称自己为kml doc.kml,并使用ZIP 2.0兼容的Deflate压缩。请您提供wiki链接和/或指定我需要更改的内容,谢谢;有问题吗?我用它来拉拉链。最大的问题是这是否是“传统ZIP 2.0压缩兼容”。我该如何验证这一点?@ZippyV第二个this.Response.Clear;似乎不会引起问题。