Download File from Geoprocessing Service by URL

2558
4
Jump to solution
12-10-2019 06:27 AM
LukasWürsch
New Contributor III

Hi,

I want to download a file created by a Geoprocessing Service. For testing, I created a Service from the script below. The script is having a user supplied input of type String and a derived output of type File. 

import os
input_from_client = arcpy.GetParameterAsText(0)
file = open("testfile.txt","w") 
file.write(input_from_client)
file.close()
arcpy.SetParameter(1, file)

I thought that I can simply set the created file as output parameter (arcpy.SetParameter) and would then be able to download it with the returned url.

Yet, when running the service with the REST interface, it returns an incorrect url (note the <_io.TextIOWrapper name='testfile.txt' mode='w' encoding='cp1252'> part in the end):

{
 "paramName": "File",
 "dataType": "GPDataFile",
 "value": {
   "url": "https://[nameofserver]/gis/rest/directories/arcgisjobs/excelreportservice_gpserver/j3ddfaa172cc7477288253c965e3c3aba/scratch/<_io.TextIOWrapper name='testfile.txt' mode='w' encoding='cp1252'>"
  }
}

Any ideas how I can get the correct download url?

Would be very thankful for support on this.

Regards, Lukas

0 Kudos
1 Solution

Accepted Solutions
JonathanQuinn
Esri Notable Contributor

You'll need to get the path from the TextIOWrapper class:

Your etParameterAsText line would be

arcpy.SetParameter(1, file.name)

View solution in original post

4 Replies
JonathanQuinn
Esri Notable Contributor

You'll need to get the path from the TextIOWrapper class:

Your etParameterAsText line would be

arcpy.SetParameter(1, file.name)
LukasWürsch
New Contributor III

Thank you for the reply. I changed the service the way you suggested and this could remove the text wrapper from the url. Now I am getting this url

https://[nameofserver]/gis/rest/directories/arcgisjobs/excelreportservice_gpserver/j08439162107846f99bc04281f6aa0a36/scratch/test.txt

I had a look at the server and noticed that the scratch folder is empty. Do I have to change the script so that the textfile is put to the scratch folder? 

0 Kudos
LukasWürsch
New Contributor III

I think i figured it out, 

The file has to be created in the scratch folder using arcpy.env.scratchWorkspace variable, then it can be downloaded with the returned url as expected

import os

input_from_client = arcpy.GetParameterAsText(0)
file = open(arcpy.env.scratchWorkspace+ "/" + "test.txt","w")
file.write(input_from_client)
file.close()
arcpy.SetParameter(1, file.name)

 

Arne_Gelfert
Occasional Contributor III

Thanks, that was very helpful!

0 Kudos