Skip to content

Instantly share code, notes, and snippets.

@phpmaps
Created March 26, 2014 20:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save phpmaps/9791947 to your computer and use it in GitHub Desktop.
Save phpmaps/9791947 to your computer and use it in GitHub Desktop.
Upload file to Geoprocessing Service
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<title>Upload Proxy Issue</title>
<link rel="stylesheet" href="http://js.arcgis.com/3.8/js/esri/css/esri.css">
<style>
html, body, #map {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
body {
background-color: #FFF;
overflow: hidden;
font-family: "Trebuchet MS";
}
</style>
<script src="http://js.arcgis.com/3.8/"></script>
<script>
var map, gpTask;
require(["esri/urlUtils","esri/map", "esri/tasks/Geoprocessor", "esri/arcgis/utils", "esri/request", "dojo/dom", "dojo/on", "dojo/domReady!"], function(urlUtils, Map, Geoprocessor, arcgisUtils, esriRequest, dom, on) {
map = new Map("map", {
basemap: "topo",
center: [-122.45,37.75], // long, lat
zoom: 13,
sliderStyle: "small"
});
urlUtils.addProxyRule({
urlPrefix : "http://sampleserver6.arcgisonline.com/arcgis/rest/services",
proxyUrl : "proxy.php"
});
//esriConfig.defaults.io.proxyUrl = "proxy.php";
//esriConfig.defaults.io.alwaysUseProxy = true;
on(dom.byId("upload"), "click", upload);
function upload()
{
var upload = esriRequest({
url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/911CallsHotspot/GPServer/uploads/upload",
form: dojo.byId("uploadForm"),
content: { f: "pjson" },
handleAs: "json",
load: displayResult,
error: uploadFailed
});
//upload.then(uploadSucceeded, uploadFailed);
}
function uploadSucceeded(response, io) {
var itemID= response["item"].itemID;
//alert(itemID)
var params= { "uploadSHPFile": "{'itemID':" + itemID + "}" };
gpTask = new Geoprocessor("http://sampleserver6.arcgisonline.com/arcgis/rest/services/911CallsHotspot/GPServer/uploads/upload");
gpTask.setOutSpatialReference({wkid:102100});
gpTask.execute(params, displayResult, errorGP);
}
function uploadFailed(){
alert("Failed!")
}
function displayResult(evt) {
alert("succeed");
console.log(evt);
}
function errorGP(){
//alert("fail");
}
/*gp = new Geoprocessor("http://sampleserver6.arcgisonline.com/arcgis/rest/services/911CallsHotspot/GPServer/uploads/upload");
var dataFile = new esri.tasks.DataFile();
dataFile.url = "http://upload.wikimedia.org/wikipedia/commons/4/49/Kap_Sounion.jpg";
var params = { "Input_File": dataFile };
gp.submitJob(params);*/
});
</script>
</head>
<body>
<form id="uploadForm" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="hidden" name="description" />
</form>
<input type="button" value="Upload" id="upload"/>
<div id="map"></div>
</body>
</html>
@esride-mag
Copy link

Great work, Thanks!

@amitksinha
Copy link

Good.

@bsnider
Copy link

bsnider commented Nov 2, 2017

Nice sample, Doug! I'm trying to run the sample, but I'm hitting an error. Using your upload function as is:

var upload = esriRequest({
          url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/911CallsHotspot/GPServer/uploads/upload",
          form: dojo.byId("uploadForm"),
          content: {
            f: "pjson"
          },
          handleAs: "json",
          load: displayResult,
          error: uploadFailed
        });

I receive the following error, because the code is expecting a JSON response, not html: SyntaxError: Unexpected token <
But, looking at the rest service, it can only return html.

If I change the function to

var upload = esriRequest({
          url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/911CallsHotspot/GPServer/uploads/upload",
          form: dojo.byId("uploadForm"),
          content: {
            f: "html"
          },
          handleAs: "html",
          load: displayResult,
          error: uploadFailed
        });

I receive the error TypeError: t[a.ioArgs.handleAs] is not a function

Any ideas for fixing the code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment