<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<!--ajaxfileupload.js是用于文件上传的jQuery插件 -->
<script type="text/javascript" src="ajaxfileupload.js"></script>
<script>
//iServer服务器根地址
var rootUrl="http://localhost:8090/iserver";
//对iServer服务器有上传权限的token
var token="8Gwvi2W3Re4w4kXHfJ7dRD3C3gEgugCg13DQqgEDrusuPrmPWJ7gGWeEcs4y_TRLKdkKEcQxX_XODXECfd1vhA..";
//上传的目标路径
var destDir = "../../samples/data/test/hehe.smwu";
var updateProgressTimer;
var lastProgress = -1;
var proxyPath="http://localhost:8080/ArcGis_Proxy_Java/proxy.jsp?";
function ajaxFileUploadTask(callback) {
var uploadTaskURL="";
$.ajax({
url: proxyPath+rootUrl+'/manager/filemanager/uploadtasks.json?token='+ token,
type: 'POST',
data: {},
dataType: 'json',
//超时时间,单位:毫秒
timeout: 60*1000,
success: function(data, status){
$('#result').prepend('<p>创建上传任务成功,任务ID为:'+ data.newResourceID + '</p>');
$('#progressP').show();
uploadTaskURL = data.newResourceLocation + ".json";
uploadURL = uploadTaskURL + "?toFile="+destDir+"&unzip=false&overwrite=true&token="+ token;
ajaxFileUpload(callback, uploadURL);
updateProgressTimer = setInterval(function(){
$.ajax({
url: uploadTaskURL + "?token=" + token,
success: function(data, status, e){
if(data.progress == null){
clearInterval(updateProgressTimer);
return;
}
$('#progress').html(data.progress+"%");
if(data.progress >= 100){clearInterval(updateProgressTimer);}
},
error: function(data, status, e){
if(data && data.status == 404){
clearInterval(updateProgressTimer);
}
}
});
}, 500);
},
error: function(xmlHttpRequest, status, e){
$('#result').prepend('<p>创建上传任务失败</p>');
}
})
return false;
}
function ajaxFileUpload(callback, uploadURL){
$.ajaxFileUpload({
url: uploadURL,
secureuri:false,
fileElementId:'fileSelect',
dataType: 'json',
timeout: 60*60*1000,
success: function (data, status)
{
$('#result').append('上传成功');
},
error: function (data, status, e)
{
$('#result').append('上传失败');
throw(e);
}
})
}
function execute(callback){
if($('#fileSelect').val()==""){
alert("请指定要上传的文件。");
return;
}
ajaxFileUploadTask(callback);
}
</script>
</head>
<body>
本地文件:<input type="file" id="fileSelect" name="file" title="选择文件"/><br/>
<input type="button" value="Upload" onclick="execute()"/>
<div id="result">
<p id="progressP" style="display:none">上传进度:<label id="progress">0%</label></p>
</div>
</body>
</html>
为什么会在xhr.send( ( s.hasContent && s.data ) || null );同时出现下面2个错误:
Failed to load resource: the server responded with a status of 401 (Unauthorized)
和 Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING