How to download file generated by custom export tool in Web Player to the user's machine

How to download file generated by custom export tool in Web Player to the user's machine

book

Article ID: KB0076425

calendar_today

Updated On:

Products Versions
Spotfire Web Player All Versions

Description

The custom export tool in Web Player is executed on the Web Player server and thus the files are generated in Web Player Server. To provide the ability for the users to download these files you can do one of the following,

 

Issue/Introduction

How to download the files generated by a custom export tool when executed in Web Player to the user's machine who executed the tool.

Resolution

1). In the ExecuteCore (Document document,ExportResult result) of a CustomExportTool, you can add your result to the ExportResult which is a container with the parts representing the result of an export tool.

Example:
protected override IEnumerable<object> ExecuteCore(Document document, ExportResult result)
{
  ExportResultPart resultPart = result.AddPart(text + ".txt", new ContentType("text/plain"));
                using (StreamWriter writer = new StreamWriter(resultPart.GetStream()))
                {
                    writer.WriteLine("Hello");
                    writer.WriteLine("World!");
                }
 }
 
2). You can host the files created in the Web Player server in IIS and then create a text area in the analysis with a link to the file. This would work only with CustomTools<Tcontext> and not with CustomExportTool. This is because CustomExportTools are always executed inside what we call a "transient transactions". This means that any modifications of the document are roll-backed after execution of the tool.
 
Example:
public class SampleTool : CustomTool<Document>{
 protected override IEnumerable<object> ExecuteCore(Document document)
 {
    string varurl = "http://google.com";
    Page activePage = document.ActivePageReference;
    bool exists = false;
    foreach (Visual viz in activePage.Visuals)
    {
        if (viz.Title.Equals("Link to download"))
        {
            viz.As<HtmlTextArea>().HtmlContent = "<a href = \"" + varurl + "\">Click to open link" + "</a>";
        }

    }
    if (!exists)
    {
        Visual dynamic = activePage.Visuals.AddNew(VisualTypeIdentifiers.HtmlTextArea);
        dynamic.Title = "Link to download";
        dynamic.As<HtmlTextArea>().HtmlContent = "<a href = \"" + varurl + "\">Click to open link" + "</a>";
    }
 }
}

 

Additional Information

Check the HtmlPrintToolExample available with the SDK examples
Doc: ExportTool.ExecuteCore() Doc: ExportResult Doc: CustomTool