dojo 如何在不使用文件输入的情况下将zip文件传递给函数?

uz75evzq  于 2022-12-16  发布在  Dojo
关注(0)|答案(1)|浏览(162)

如何在不使用uploadForm的情况下将位于服务器(../maps/GIS.ZIP)上的GIS.ZIP文件传递到generateFeatureCollection()
如您所见,我当前正在调用Dojo on函数:

on(dom.byId("uploadForm"), "change", function (event) {});

上传并将a zip文件传递到generateFeatureCollection(),但我希望在加载时执行此操作

on(dom.byId("uploadForm"), "change", function (event) {
        var fileName = event.target.value.toLowerCase();

        if (sniff("ie")) { //filename is full path in IE so extract the file name
          var arr = fileName.split("\\");
          fileName = arr[arr.length - 1];
        }
        if (fileName.indexOf(".zip") !== -1) {//is file a zip - if not notify user
          generateFeatureCollection(fileName);
        }
        else {
          dom.byId('upload-status').innerHTML = '<p style="color:red">Add shapefile as .zip file</p>';
        }
      });

     function generateFeatureCollection (fileName) {
        var name = fileName.split(".");
        //Chrome and IE add c:\fakepath to the value - we need to remove it
        //See this link for more info: http://davidwalsh.name/fakepath
        name = name[0].replace("c:\\fakepath\\", "");

        dom.byId('upload-status').innerHTML = '<b>Loading… </b>' + name;

        //Define the input params for generate see the rest doc for details
        //http://www.arcgis.com/apidocs/rest/index.html?generate.html
        var params = {
          'name': name,
          'targetSR': map.spatialReference,
          'maxRecordCount': 1000,
          'enforceInputFileSizeLimit': true,
          'enforceOutputJsonSizeLimit': true
        };

        //generalize features for display Here we generalize at 1:40,000 which is approx 10 meters
        //This should work well when using web mercator.
        var extent = scaleUtils.getExtentForScale(map, 40000);
        var resolution = extent.getWidth() / map.width;
        params.generalize = true;
        params.maxAllowableOffset = resolution;
        params.reducePrecision = true;
        params.numberOfDigitsAfterDecimal = 0;

        var myContent = {
          'filetype': 'shapefile',
          'publishParameters': JSON.stringify(params),
          'f': 'json',
          'callback.html': 'textarea'
        };

        //use the rest generate operation to generate a feature collection from the zipped shapefile
        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;
            dom.byId('upload-status').innerHTML = '<b>Loaded: </b>' + layerName;
            addShapefileToMap(response.featureCollection);
          }),
          error: lang.hitch(this, errorHandler)
        });
      }
6qfn3psc

6qfn3psc1#

由于浏览器的安全性,你不能自动选择一个文件,因此在IE的情况下,只有你可以创建一个函数加载(dojo/ready),这将模拟点击一个隐藏的输入文件(display:nonevisibility:hidden输入),这将打开一个窗口来选择您的Zip文件,否则它是不可能在其他浏览器如Chrome或Firefox。

相关问题