[英]Download XML file with JavaScript and C#/Rest
I have an xml document that I generated in C#, I would like to return the string/document via WCF/REST so it will be downloaded by the browser. 我有一个用C#生成的xml文档,我想通过WCF / REST返回字符串/文档,以便浏览器将其下载。 What is the operationcontract/return type that I should use?
我应该使用什么操作合同/退货类型? And how can I get it to be prompted to save by javascript and the browser.
以及如何让它被JavaScript和浏览器提示保存。
I had the similar issue with NodeJS backend. 我在NodeJS后端遇到类似的问题。
I returned XML as a string and then on front-end I used next code: 我以字符串形式返回XML,然后在前端使用下一个代码:
<a href="data:text/xml,HERE WILL BE YOUR XML" download="filename.xml">Download</a>
Your operation contract should not be one way and you should return Stream
您的运营合同不应是一种方式,而应返回
Stream
[OperationContract(IsOneWay = false)]
[WebGet(UriTemplate = "GetXml/{xmlFileName}")]
Stream GetXml(string xmlFileName);
public Stream GetXml(string xmlFileName)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
string xmlLocation=GetXmlLocation(xmlFileName);
try
{
return File.OpenRead(xmlLocation);
}
catch
{
// File Not Found
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.