Download a zip file with an ArcPY geoprocessing service in ArcGIS Portal

1438
10
Jump to solution
06-24-2020 10:28 AM
JeffHanson2
New Contributor III

I have an ExportExcelFile geoprocessing service developed in ArcPy and I want the user to get the download file URL returned so that they can click on the link and download the Excel file.   The geoprocessing service is called from a Portal Web App Geoprocessing widget.   I can get the string result but that needs to be copied and pasted into another browser tab to work.  I just want the user to click on the download URL link.   I am returning an output parameter of type "File" but only getting an object [object] in the results.   Here is the code:

import arcpy, os, json, sys, zipfile, urllib

arcpy.env.overwriteOutput = True

####################################
### setting variable default values
####################################

arcpy.env.scratchWorkspace = "//HERTZGIS/directories/arcgisoutput/PortalShapefiles/"

# Shapefile output folder
exportFolder = "//HERTZGIS/directories/arcgisoutput/PortalShapefiles/"
arcpy.AddMessage("Output folder is " + exportFolder)

#############################
### Getting input parameters
#############################

arcpy.AddMessage("  Collecting input parameters")

# Get the passed field polygon graphics and user specified shapefile name
inFeatset = arcpy.GetParameter(0)
if inFeatset == '#' or not inFeatset:
    sys.exit()

xlsFileName = arcpy.GetParameterAsText(1)
outputExcelFile = exportFolder + xlsFileName + ".xls"
if not xlsFileName:
    sys.exit()
arcpy.AddMessage("Output file is " + outputExcelFile)

#############################
### start main proccess
#############################

try:
    arcpy.AddMessage("Running shapefile export to create " + outputExcelFile)

    arcpy.TableToExcel_conversion(inFeatset, outputExcelFile)

    arcpy.AddMessage("Created shapefile: " + xlsFileName + ".xls")

    # put the files that make up the shapefile into a zip file
    zip = zipfile.ZipFile(exportFolder + xlsFileName + ".zip"'w', zipfile.ZIP_DEFLATED)
    for file in os.listdir(exportFolder):
        if file.startswith(xlsFileName):
            if not file.endswith(".zip":(
                print(os.path.join(exportFolder, file))
                zip.write(os.path.join(exportFolder, file))
    zip.close()        
    
    vOutputFile = "https://HERTZGIS/PortalShare/" + xlsFileName + ".xls"
    aFile = open(vOutputFile)
    aFile.close()

    arcpy.AddMessage(vOutputFile)

    arcpy.SetParameter(2, aFile)
except:
    arcpy.AddMessage("Error exporting and downloading shapefile " + outputExcelFile)
10 Replies
LanceKirby2
Occasional Contributor

Thanks for the tip. I will try using SetParameter instead. I believe the problem lies with the argument being sent to the gp service and not what is returned. I take it that you aren’t experiencing the issue I am describing above?

0 Kudos