2017-04-17 121 views
0

我能夠實現圖片上傳例子的TinyMCE的文檔中找到顯示錯誤信息發生錯誤?例如,如果我上傳了無效的文件類型,那隻能說明這樣一個通用的500錯誤消息:TinyMCE的:在圖片上傳

enter image description here

我如何可以顯示一個更具體的錯誤消息像 「無效擴展用戶「?

回答

0

嗨,需要編寫你自定義的圖片_upload_handler。 在設置中添加以下行:

images_upload_handler : function handler(blobInfo, success, failure, progress) { 
{ 
     var valid_extensions = ['png','jpg'] 
     var ext, extensions; 

     extensions = { 
      'image/jpeg': 'jpg', 
      'image/jpg': 'jpg', 
      'image/gif': 'gif', 
      'image/png': 'png' 
     }; 
     ext = extensions[blobInfo.blob().type.toLowerCase()] || 'dat'; 
     //add your extension test here. 
     if(valid_extensions.indexOf(ext) == -1){ 
      failure("Invalid extension"); 
       return; 
     } 

     var xhr, formData; 

     xhr = new XMLHttpRequest(); 
     xhr.open('POST', settings.url); 
     xhr.withCredentials = settings.credentials; 

     xhr.upload.onprogress = function(e) { 
      progress(e.loaded/e.total * 100); 
     }; 

     xhr.onerror = function() { 
      failure("Image upload failed due to a XHR Transport error. Code: " + xhr.status); 
     }; 

     xhr.onload = function() { 
      var json; 

      if (xhr.status != 200) { 
       failure("HTTP Error: " + xhr.status); 
       return; 
      } 

      json = JSON.parse(xhr.responseText); 

      if (!json || typeof json.location != "string") { 
       failure("Invalid JSON: " + xhr.responseText); 
       return; 
      } 

      success(pathJoin(settings.basePath, json.location)); 
     }; 

     formData = new FormData(); 
     formData.append('file', blobInfo.blob(), blobInfo.filename()); 

     xhr.send(formData); 
    } 
}