7

Sitecore SheerResponse.Download Error with XML

 3 years ago
source link: https://blog.coates.dk/2016/12/20/sitecore-sheerresponse-download-error-with-xml/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Sitecore SheerResponse.Download Error with XML

I had  a solution where we need to generate an XML file and download it from the Sitecore client. But when using SheerResponse.Download to download an XML file, it would add 2 elements to the end of the XML document (see the image below). The issue was found in sitecore 8.1 rev. 151207 (Update-1).

extra-elements

Sitecore have registered this a bug and provided a solution where I had to modify the main layout for the Sitecore client and use several support DLL’s, I decided not to do this.

Solution

I therefore did what I consider to be a nasty hack, but better than the alternative of modifying the Sitecore client, so please do not judge me for this!

SheerUI has a Eval function, which allows you to execute JavaScript within the Sitecore client. So I added an A tag, with a link to the file and then called click on the link.

internal void DownloadFile([NotNull] FileInfo file)
{
Assert.ArgumentNotNull(file, "file");
var virtualPath = file.FullName;
var rootPath = HostingEnvironment.MapPath("/");
if (rootPath == null)
return;
virtualPath = virtualPath.Replace(rootPath, string.Empty);
virtualPath = $"/{virtualPath.Replace("\\", "/")}";
string js = $"var link=document.createElement('a');document.body.appendChild(link);link.href = '{virtualPath}';link.download='{file.Name}';link.click();";
SheerResponse.Eval(js);
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK