Skip to main content
1-Visitor
December 19, 2019
Solved

Snippet to download a file via a service?

  • December 19, 2019
  • 4 replies
  • 4596 views

Is there a code snippet to download a file from a repository? I'm looking to download a file from within a service.

I want to avoid having to use a widget (e.g. link or data export), and wondered if there was any way to kick off a download from within a script. I realise I can use the GetFileListingsWithLinks to get the url, but how can I then activate that url from within a service.

 

What I actually want to happen is: on the click of a button I want to run a service that creates a file, writes some data (based on some user defined parameters), saves it, then automatically begin the download. Ideally after the download I would like to delete the file.

 

TIA

K

 

Best answer by posipova

You could use content loader functions to utilize REST calls for export:

https://www.ptc.com/en/support/article?n=CS224211

(using content loader functions:  https://www.ptc.com/en/support/article?n=CS237875 )

 

You'd use the value/URL obtained previously.

4 replies

20-Turquoise
December 19, 2019

Hi, 

 

With what we have out of the box, you can either create a service on your FileRepository thingA or on another thing and reference thingA (when creating a service, go to Me/Entities and select the entity). Then you could do something like this:

 

// result: INFOTABLE dataShape: "FileSystemFileWithLinks"
var result = Things["TestingRepo"].GetFileListingWithLinks({
path: undefined /* STRING */,
nameMask: undefined /* STRING */
});

 

and place the result into a grid to select the URL, or write another service to fetch the url from the infotable result of the first service.

 

khayes11-VisitorAuthor
1-Visitor
December 20, 2019

Hi,

thanks for the reply. I'm trying to find a solution which doesn't involve using a grid if possible. I understand your suggestion about writing a service to return the link, but the bit I am struggling with is how to initiate the download from within the service once I have the download link.

 

Ideally what I want to do is have a single button which will generate a file and then automatically begin to download that file to my PC.

posipova20-TurquoiseAnswer
20-Turquoise
December 20, 2019

You could use content loader functions to utilize REST calls for export:

https://www.ptc.com/en/support/article?n=CS224211

(using content loader functions:  https://www.ptc.com/en/support/article?n=CS237875 )

 

You'd use the value/URL obtained previously.