Javascript 如何在不使用文件输入的情况下将zip文件传递到函数中?
如何在加载时将服务器上的Javascript 如何在不使用文件输入的情况下将zip文件传递到函数中?,javascript,jquery,dojo,Javascript,Jquery,Dojo,如何在加载时将服务器上的GIS.ZIP文件(./maps/GIS.ZIP)传递给generatefeaturecolection(),而不使用上传表单 如您所见,我当前正在调用Dojoon函数: on(dom.byId("uploadForm"), "change", function (event) {}); 要上载azip文件并将其传递到generateFeatureCollection(),我希望在加载时执行此操作 on(dom.byId("uploadForm"), "c
GIS.ZIP
文件(./maps/GIS.ZIP
)传递给generatefeaturecolection()
,而不使用上传表单
如您所见,我当前正在调用Dojoon
函数:
on(dom.byId("uploadForm"), "change", function (event) {});
要上载azip
文件并将其传递到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)
});
}
on(dom.byId(“uploadForm”),“change”,函数(事件){
var fileName=event.target.value.toLowerCase();
如果(嗅探(“ie”){//filename是ie中的完整路径,则提取文件名
var arr=fileName.split(“\\”);
fileName=arr[arr.length-1];
}
if(fileName.indexOf(“.zip”)!==-1){//是一个zip文件-如果不是,通知用户
generateFeatureCollection(文件名);
}
否则{
dom.byId('upload-status').innerHTML='将shapefile添加为.zip文件;
}
});
函数generateFeatureCollection(文件名){
var name=fileName.split(“.”);
//Chrome和IE将c:\fakepath添加到值中-我们需要删除它
//有关更多信息,请参阅此链接:http://davidwalsh.name/fakepath
名称=名称[0]。替换(“c:\\fakepath\\”,“”);
dom.byId('upload-status').innerHTML='Loading…'+name;
//定义generate的输入参数,详见rest单据
//http://www.arcgis.com/apidocs/rest/index.html?generate.html
变量参数={
“名称”:名称,
“targetSR”:map.spatialReference,
“maxRecordCount”:1000,
“enforceInputFileSizeLimit”:true,
“enforceOutputJsonSizeLimit”:真
};
//此处显示的概括功能我们概括为1:40000,约10米
//这在使用WebMercator时应该可以很好地工作。
var extent=scaleUtils.getExtentForScale(映射,40000);
var resolution=extent.getWidth()/map.width;
params.generalize=true;
params.maxAllowableOffset=分辨率;
params.reducePrecision=true;
params.numberOfDigitsAfterDecimal=0;
var myContent={
“文件类型”:“shapefile”,
“publishParameters”:JSON.stringify(params),
‘f’:‘json’,
'callback.html':'textarea'
};
//使用rest生成操作从压缩的形状文件生成要素集合
请求({
url:portalUrl+'/sharing/rest/content/features/generate',
内容:myContent,
表单:dom.byId('uploadForm'),
handleAs:'json',
加载:lang.hitch(此,功能(响应){
if(response.error){
errorHandler(response.error);
回来
}
var layerName=response.featureCollection.layers[0].layerDefinition.name;
dom.byId('upload-status').innerHTML='Loaded:'+layerName;
addShapefileToMap(response.featureCollection);
}),
错误:lang.hitch(此为errorHandler)
});
}
由于浏览器的安全性,您无法自动选择文件,因此在IE的情况下,您只能在加载时创建一个函数(dojo/ready)
,该函数将模拟单击隐藏的输入文件(显示:无
或可见性:隐藏
输入),这将打开一个窗口来选择您的Zip文件,否则在其他浏览器如Chrome或Firefox中是不可能的