You need to sign in or sign up before continuing.
fileUpload.js 2.56 KB
import axios from 'axios'
export const BASE_HOST = "/"
export const BASE_URI = BASE_HOST + "";

const instance = axios.create({
  baseURL: BASE_URI,
  timeout: 5000,
  method: 'post',
});

export default class FileUpload {
  static fileUpload(url, data = {}, params = {}) {
    return this._base(url, 'post', data, params)
  }

  static fileDownload(url) {
    return this._base(url, "get", {}, "");
  }

  static _base(url, method, data, params, headers = {
    'Content-Type': 'multipart/form-data'
  }) {
    return new Promise((resolve, reject) => {
      axios({
        url: BASE_URI + url,
        method: method,
        data,
        params,
        timeout: 50000,
        headers: headers
      }).then(response => {
        const { data } = response;
        resolve(data)
      })
        .catch(error => {
        });
    });
  }

  static pget(url, data) {
    let token = window.sessionStorage.token
    let userId = window.sessionStorage.userId
    let headers = { userId, token, "terminal": "WEB" };
    if (url != `${BASE_URI}login` && token)
      headers.token = token;
    return new Promise((resolve, reject) => {
      axios.get(url, data, {
        headers,

        timeout: 50000,
      })
        .then(response => {
          const { data } = response;
          resolve(data)
        })
        .catch(error => {
          if (error.response) {
            // The request was made and the server responded with a status code
            // that falls out of the range of 2xx
            console.log(error.response.data);
            console.log(error.response.status);
            console.log(error.response.headers);
            resolve(error.response.status);
          } else if (error.request) {
            // The request was made but no response was received
            // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
            // http.ClientRequest in node.js
            console.log(error.request);
            resolve('error.request');
          } else {
            // Something happened in setting up the request that triggered an Error
            console.log('Error', error.message);
            resolve(error.message);
          }
          console.log(error.config);
        });

    })
  }

  static get(url, data) {
    return this.pget(BASE_HOST + url, data)
  }

  static downloadFile(excelObj, filename) {
    let url = document.createElement('a');
    url.download = filename;
    url.style.display = "none";
    url.href = excelObj;
    document.body.appendChild(url);
    url.click()
    document.body.removeChild(url);
  }
}