cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Snippet to download a file via a service?

khayes1
13-Aquamarine

Snippet to download a file via a service?

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
posipova
20-Turquoise
(To:khayes1)

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.

View solution in original post

4 REPLIES 4
posipova
20-Turquoise
(To:khayes1)

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.

 

khayes1
13-Aquamarine
(To:posipova)

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.

posipova
20-Turquoise
(To:khayes1)

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.

That is very helpful! thank you 

run 3

Top Tags