本文介绍了如何生成ASP.NET中的KML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何生成,并直接返回KML文件浏览器,而无需编写一个临时文件到服务器或依赖第三方库或类?


解决方案

我建议你考虑使用一个HTTP处理程序,而不是一个ASP.NET页面。这将是更清洁和更高性能。只需键入通用处理器的新项目添加到您的项目,并考虑将code到的ProcessRequest 方法。一般的方法是不错,虽然。

顺便说一句,除非你明确地映射 .KML 文件到ASP.NET处理程序,它会不反正运行。我建议使用默认 ashx的延长会,并添加内容处置 HTTP头设置文件名客户端

  Response.AddHeader(内容处置,附件;文件名= File.kml);

另外请注意,您应该设置页眉东西的之前什么是发送到客户端,所以你应该移动到设定内容类型并添加页眉之前其他的东西。


完整的解决方案(从OP):

下面是我是如何做到的:

服务器


  1. 的MIME类型.KML添加到您想要这个文件住的文件夹。比方说, \\\\ myDevServer \\ ... \\的Inetpub \\ KML 结果
    ()

    1. 您的DEV服务器上打开 Internet信息服务(IIS)管理器

    2. 导航到您的DEV网站

    3. 右键单击 KML 文件夹并选择属性

    4. 点击 HTTP标题标签

    5. 点击 MIME类型按钮

    6. 点击

    7. 输入

      • 扩展:.KML

      • MIME类型:application / vnd.google-earth.kml + XML


    8. 点击确定两次以返回到 HTTP标题标签


  2. KML 文件夹作为一个ASP.NET应用程序(可能可选取决于服务器的设置方式)

    1. 点击目录标签

    2. 点击创建按钮

    3. 应用程序名字段变为与设置生效 KML

    4. 点击确定带你回到主IIS管理器窗口


网站


  1. 开启VS2008:

    1. 文件>>新网站

    2. 选择:

      • 空白网站

      • 语言: C#

      • 地点: \\\\ myDevServer \\ ... \\的Inetpub \\ KML \\



  2. 解决方案管理器

    1. 单击鼠标右键的网站

    2. 选择新项目

    3. 选择通用处理器 Visual Studio已安装的模板窗口

    4. 输入一个名称(我用 MelroseVista.ashx

    5. 选择语言:的Visual C#

    6. 点击确定


  3. 粘贴下面code

//

 使用系统;
使用的System.Web;
使用的System.Xml;公共类处理程序:IHttpHandler的
{
    公共无效的ProcessRequest(HttpContext的背景下)
    {
        context.Response.ContentType =应用程序/ vnd.google-earth.kml + XML;
        context.Response.AddHeader(内容处置,附件;文件名= MelroseVista.kml);        XmlTextWriter的KML =新的XmlTextWriter(context.Response.OutputStream,System.Text.Encoding.UTF8);        kml.Formatting = Formatting.Indented;
        kml.Indentation = 3;        kml.WriteStartDocument();        kml.WriteStartElement(KML,http://www.opengis.net/kml/2.2);
        kml.WriteStartElement(标点);
        kml.WriteElementString(姓名,玫瑰花Vista的FL);
        kml.WriteElementString(说明,一个可爱的小城镇);        kml.WriteStartElement(点);        kml.WriteElementString(坐标,-80.18451400000000000000,26.08816400000000000000,0);        kml.WriteEndElement(); //<点和GT;
        kml.WriteEndElement(); //<标>
        kml.WriteEndDocument(); //<&KML GT;        kml.Close();    }
    公共BOOL IsReusable
    {
        得到
        {
            返回false;
        }
    }
}


  1. 尝试加载您的网页你喜欢的浏览器

  2. 您的的得到一个弹出要求你打开保存所产生的KML文件。

  3. 如果您打开,你的的有GoogleEarth的自行启动并放大到东部佛罗里达州的一个图钉

  4. 如果您保存,你的的看到文件在以下

\\

 <?XML版本=1.0编码=UTF-8&GT?;
< KML的xmlns =htt​​p://www.opengis.net/kml/2.2>
   <标>
      <名称>梅尔罗斯Vista的FL< /名称>
      <描述>一种不错的小城镇< /描述>
      <点和GT;
         <&坐标GT; -80.18451400000000000000,26.08816400000000000000,0< /坐标>
      < /点和GT;
   < /标>
< / KML>

请注意:的XmlTextWriter 工作pretty以及在这里。不过,我觉得的XMLDocument 寻找更大的KML文件更有前途,因为可以将其推给用户之前在内存中进行操作。如果,例如,你想同样的点出现在GoogleEarth的位置树多个文件夹。

How do I generate and return a KML document directly to the browser without writing a temporary file to the server or relying on a 3rd party library or class?

解决方案

I suggest you consider using an HTTP Handler instead of a ASP.NET page. It will be cleaner and more performant. Just add new item of type "Generic Handler" to your project and consider moving the code to its ProcessRequest method. The general approach is good, though.

By the way, unless you are explicitly mapping .kml files to an ASP.NET handler, it'll not run anyway. I suggest going with the default .ashx extension and add a Content-Disposition HTTP header to set the filename for the client:

Response.AddHeader("Content-Disposition", "attachment; filename=File.kml");

Also, note that you should set header stuff before anything is sent to the client so you should move setting Content-Type and adding header before other stuff.


Full Solution (From the OP):

Here's how I did it:

Server

  1. Add the .kml mimetype to the folder where you want this "file" to live. Say, \\myDevServer\...\InetPub\KML
    (Google's instructions are only for Apache)

    1. Open Internet Information Services (IIS) Manager on your DEV server
    2. Navigate to your DEV site
    3. Right-click the KML folder and choose Properties
    4. Click the HTTP Headers tab
    5. Click the MIME types button
    6. Click New
    7. Enter
      • Extension: .kml
      • MIME Type: application/vnd.google-earth.kml+xml
    8. Click OK twice to get back to the HTTP Headers tab

  2. Set the KML folder as an ASP.NET application (maybe optional depending on how your server is set up)

    1. Click the Directory tab
    2. Click the Create button
    3. The Application name field becomes active with the setting KML
    4. Click OK taking you back to the main IIS Manager window

Website

  1. Open VS2008:

    1. File >> New Website
    2. Choose:
      • Empty Web Site
      • Language: C#
      • Location: \\myDevServer\...\InetPub\KML\

  2. In Solution Explorer

    1. Rightclick the website
    2. Choose New Item
    3. Choose Generic Handler from the Visual Studio installed templates window
    4. Enter a name (I used MelroseVista.ashx )
    5. Choose Language: Visual C#
    6. Click OK

  3. Paste the following code

//

using System;
using System.Web;
using System.Xml;

public class Handler : IHttpHandler
{
    public void ProcessRequest( HttpContext context)
    {
        context.Response.ContentType = "application/vnd.google-earth.kml+xml";
        context.Response.AddHeader("Content-Disposition", "attachment; filename=MelroseVista.kml");

        XmlTextWriter kml = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);

        kml.Formatting = Formatting.Indented;
        kml.Indentation = 3;

        kml.WriteStartDocument();

        kml.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");
        kml.WriteStartElement("Placemark");
        kml.WriteElementString("name", "Melrose Vista   FL");
        kml.WriteElementString("description", "A nice little town");

        kml.WriteStartElement("Point");

        kml.WriteElementString("coordinates", "-80.18451400000000000000,26.08816400000000000000,0");

        kml.WriteEndElement(); // <Point>
        kml.WriteEndElement(); // <Placemark>
        kml.WriteEndDocument(); // <kml>

        kml.Close();

    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
  1. Attempt to load your page in your favorite browser
  2. You should get a popup asking you to open or save the resulting KML file.
  3. If you open it, you should have GoogleEarth launch itself and zoom to a thumbtack in Eastern Florida
  4. If you save it, you should see the following in the file

\

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
   <Placemark>
      <name>Melrose Vista   FL</name>
      <description>A nice little town</description>
      <Point>
         <coordinates>-80.18451400000000000000,26.08816400000000000000,0</coordinates>
      </Point>
   </Placemark>
</kml>

Note: XmlTextWriter worked pretty well here. However, I think XMLDocument looks more promising for larger KML files since you can manipulate it in memory before pushing it to the user. If, for example, you want the same point to appear in multiple folders in the GoogleEarth Locations tree.

这篇关于如何生成ASP.NET中的KML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 07:14