gpt4 book ai didi

javascript - 如何在不使用文件输入的情况下将 zip 文件传递​​到函数中?

转载 作者:行者123 更新时间:2023-11-28 10:44:43 24 4
gpt4 key购买 nike

如何将服务器上的 GIS.ZIP 文件 (../maps/GIS.ZIP) 传递给 generateFeatureCollection()在不使用 uploadForm 的情况下加载?

如您所见,我当前正在调用 Dojo on 函数:

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

上传 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)
});
}

最佳答案

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

关于javascript - 如何在不使用文件输入的情况下将 zip 文件传递​​到函数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45494784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com