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).
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); }
My Sitecore case on this bug is 473183, which contains instructions and a DLL from support (I haven’t tried it yet). The developer next to me said that this defect has been coming and going since Sitecore 7.
The fix requires the default.aspx to be modified and I was unwilling to risk it introducing more bugs as it’s not fully tested
And where did you put this piece of code in the solution?
I’ll find out