JS读取服务器文件

  getFileFromServer(url, doneCallback) {
    
    
    var xhr;

    xhr = new XMLHttpRequest();

    xhr.onreadystatechange = handleStateChange;

    xhr.open('GET', url, true);

    xhr.send();

    function handleStateChange() {
    
    
      if (xhr.readyState === 4) {
    
    
        doneCallback(xhr.status == 200 ? xhr.responseText : null);
      }
    }
  }

调用:

this.getFileFromServer(fileUrl, (res) => {
    
    
      console.log('res:', res);
});

猜你喜欢

转载自blog.csdn.net/weixin_43363720/article/details/127964855