ssj_8551
北堂棣
采纳率100%
2016-01-28 06:41

上传文件时的错误提示如何更友好的提示?

已采纳

图片说明

如图所示,上传文件出错的提示是来自服务器的信息,如何将该错误提示用js改写为更友好的提示界面?

js代码片段:

function uploadFile(){
var strFileName = $('#file-scorm').val();
if(strFileName.length>0){
if(!IsValidFileExtention(strFileName,["zip"])){
alert("文件格式不正确!");
return false;
}else{
var url = "/server/train/course/info/update/scormstatus.json";
$.getJSON(url,{id:id},function(rtn){});
var file = document.getElementById('file-scorm').files[0];
if(file){
$('#file-name').text(file.name);
}
$('#creator_id').val(memberId);
$('#callback-url').val($.cookie('callback_url').replace(/\"/g,"") + "/server/callback/train/course/section/scorm/save.json");
var url_proxy = $.cookie('reosurce_proxy').replace(/\"/g,"");
var formData = new FormData();
formData.append("id",id);
formData.append("creator_id",memberId);
formData.append("UPLOAD-CALLBACK",$('#callback-url').val());
formData.append("UPLOAD-COMMAND","dfs,scorm");
formData.append("file",document.getElementById('file-scorm').files[0]);
$("#dialog_upload").dialog("open");
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", url_proxy);
xhr.send(formData);
}
}else{
window.location.href = "add_scorm_step3.html?id=" + id;
}
}

function uploadProgress(evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
var anchor = $('#dialog_upload');
$('.progress-bar', anchor).css('width', percentComplete.toString() + '%').html(percentComplete.toString() + '%');
}
else {
$('.progress-bar', "#dialog_upload").css('width','0%').html('0%');
$('#file-status').text("上传失败");
}
}

function uploadComplete(evt) {
/* 服务器端返回响应时候触发event事件*/
if(evt.target.status==200){
$("#dialog_upload").dialog("close");
window.location.href = "add_scorm_step3.html?id=" + id;
}else{
$('.progress-bar', "#dialog_upload").css('width','0%').html('0%');
$('#file-status').text("上传失败");
alert(evt.target.responseText);
}
}
function uploadFailed(evt) {
alert("文件上传失败.");
}
function uploadCanceled(evt) {
alert("文件上传被取消.");
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • showbo GoCityPass新加坡曼谷通票 5年前
    
    
        function uploadComplete(evt) {
            /* 服务器端返回响应时候触发event事件*/
            if (evt.target.status == 200) {
                $("#dialog_upload").dialog("close");
                window.location.href = "add_scorm_step3.html?id=" + id;
            } else {
                $('.progress-bar', "#dialog_upload").css('width', '0%').html('0%');
                $('#file-status').text("上传失败");
                ///////alert(evt.target.responseText);//不要直接输出返回的内容
                var m =/<title>([\s\S]+?)<\/title>/i.exec(evt.target.responseText);//正则提取返回的html中的title标签内容
                alert('上传失败'+(m?m[1]:''));//有返回则输出title间内容
            }
        }
    
    点赞 1 评论 复制链接分享