问题描述
如何生成,并直接返回KML文件浏览器,而无需编写一个临时文件到服务器或依赖第三方库或类?
我建议你考虑使用一个HTTP处理程序,而不是一个ASP.NET页面。这将是更清洁和更高性能。只需键入通用处理器的新项目添加到您的项目,并考虑将code到的ProcessRequest
方法。一般的方法是不错,虽然。
顺便说一句,除非你明确地映射 .KML
文件到ASP.NET处理程序,它会不反正运行。我建议使用默认 ashx的
延长会,并添加内容处置
HTTP头设置文件名客户端
Response.AddHeader(内容处置,附件;文件名= File.kml);
另外请注意,您应该设置页眉东西的之前什么是发送到客户端,所以你应该移动到设定内容类型
并添加页眉之前其他的东西。
完整的解决方案(从OP):
下面是我是如何做到的:
服务器
- 的MIME类型.KML添加到您想要这个文件住的文件夹。比方说,
\\\\ myDevServer \\ ... \\的Inetpub \\ KML
结果
()- 您的DEV服务器上打开
Internet信息服务(IIS)管理器
- 导航到您的DEV网站
- 右键单击
KML
文件夹并选择属性
- 点击
HTTP标题
标签 - 点击
MIME类型
按钮 - 点击
新
- 输入
- 扩展:.KML
- MIME类型:application / vnd.google-earth.kml + XML
- 点击
确定
两次以返回到HTTP标题
标签
- 您的DEV服务器上打开
- 将
KML
文件夹作为一个ASP.NET应用程序(可能可选取决于服务器的设置方式)- 点击
目录
标签 - 点击
创建
按钮 - 的
应用程序名
字段变为与设置生效KML
- 点击
确定
带你回到主IIS管理器窗口
- 点击
网站
- 开启VS2008:
- 文件>>新网站
- 选择:
-
空白网站
- 语言:
C#
- 地点:
\\\\ myDevServer \\ ... \\的Inetpub \\ KML \\
-
- 在
解决方案管理器
- 单击鼠标右键的网站
- 选择
新项目
- 选择
通用处理器
从Visual Studio已安装的模板
窗口 - 输入一个名称(我用
MelroseVista.ashx
) - 选择语言:
的Visual C#
- 点击
确定
- 粘贴下面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;
}
}
}
- 尝试加载您的网页你喜欢的浏览器
- 您的应的得到一个弹出要求你
打开
或保存
所产生的KML文件。 - 如果您
打开
,你的应的有GoogleEarth的自行启动并放大到东部佛罗里达州的一个图钉 - 如果您
保存
,你的应的看到文件在以下
\\
<?XML版本=1.0编码=UTF-8&GT?;
< KML的xmlns =http://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
- 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)- Open
Internet Information Services (IIS) Manager
on your DEV server - Navigate to your DEV site
- Right-click the
KML
folder and chooseProperties
- Click the
HTTP Headers
tab - Click the
MIME types
button - Click
New
- Enter
- Extension: .kml
- MIME Type: application/vnd.google-earth.kml+xml
- Click
OK
twice to get back to theHTTP Headers
tab
- Open
- Set the
KML
folder as an ASP.NET application (maybe optional depending on how your server is set up)- Click the
Directory
tab - Click the
Create
button - The
Application name
field becomes active with the settingKML
- Click
OK
taking you back to the main IIS Manager window
- Click the
Website
- Open VS2008:
- File >> New Website
- Choose:
Empty Web Site
- Language:
C#
- Location:
\\myDevServer\...\InetPub\KML\
- In
Solution Explorer
- Rightclick the website
- Choose
New Item
- Choose
Generic Handler
from theVisual Studio installed templates
window - Enter a name (I used
MelroseVista.ashx
) - Choose Language:
Visual C#
- Click
OK
- 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;
}
}
}
- Attempt to load your page in your favorite browser
- You should get a popup asking you to
open
orsave
the resulting KML file. - If you
open
it, you should have GoogleEarth launch itself and zoom to a thumbtack in Eastern Florida - 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文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!