Select to view content in your preferred language

How to Pass File Directly to esriRequest Instead of Using Form Upload

2165
4
08-03-2017 11:28 PM
BehrouzHosseini
Regular Contributor

The esriRequest is used for retrieving data from a remote server or uploading a file. The Request has a Form option which is used If the request is to upload a file. We specify the form element that contains the file input control here (As below sample).

request({
    url: portalUrl + '/sharing/rest/content/features/generate',
    content: myContent,
    form: dom.byId('uploadForm'),
    handleAs: 'json',
    load: lang.hitch(this, function (response) {
    if (response.error) {
      errorHandler(response.error);
      return;
    }
    var layerName = response.featureCollection.layers[0].layerDefinition.name;
      addShapefileToMap(response.featureCollection);
    }),
    error: lang.hitch(this, errorHandler)
});‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Now, I need to pass a file directly to this options instead of using HTML File Dialog and Input. something like

var data = "www.domain.com/GIS/App.ZIP";
 request({
 ....,
 form: data,
 ....
});

Thanks

0 Kudos
4 Replies
ThomasSolow
Frequent Contributor

This workflow tends to be tricky and the documentation isn't great.  What has generally worked for me in the 4.XX version of esriRequest is creating a FormData and appending a File to it, and then passing that FormData in to the query parameter of esriRequest.

To get the file to begin with, you can access the file input element and look at the .files property to get a list of user selected files, append them to the FormData, and so on.

edit -  it looks like you're interested in doing this without a file input.  I don't think this is really possibly.  You can access a file in the browser through drag and drop or a file input.  There's no way to provide a path and get back a File object.  This would be a security issue.

0 Kudos
JohnGrayson
Esri Regular Contributor

Here's a similar question that might provide some clues: Add attachment without form node  The reason we were able to do this in the mentioned post is because we already had the file/image contents as a blob but, as mentioned by Thomas, obtaining local files directly is as security issue.  You might be able to do this if you already have the file object as a blob, maybe obtained from an external server/service or some other API.

BehrouzHosseini
Regular Contributor

Thomas & John , thank you both for replying but what I need is not loading from from client machine!  I need to pass the zip file which is sitting on the server. like referencing to a URL in ajax call

var data = "www.domain.com/GIS/App.ZIP";
0 Kudos
JohnGrayson
Esri Regular Contributor

Then it's a two step process, first you'll first have to retrieve the file from the mentioned uri (and convert it to a blob/arraybuffer if necessary), then the second step is to use some of the techniques mentioned in the post to send it directly in the form property.  How you retrieve the file from the server will depend on the server and what it allows, ideally you can retrieve the file as a blob or arraybuffer as that will simplify the process (handleAs:"blob" or handleAs:"arraybuffer").

0 Kudos