web123456

js implements file download function

  • //Download attachment
  • down(item) {
  • let that = this;
  • axios({
  • url: 'xxx',
  • method: 'xxx',
  • data: {xxx},
  • responseType: 'blob', // The data type returned by the server
  • }).then(function (res) {
  • try {
  • const objectUrl = URL.createObjectURL(new Blob([res.data]))
  • const link = document.createElement('a')
  • link.download = decodeURIComponent(res.headers['content-disposition'].split(';')[1].split('filename=')[1])
  • link.href = objectUrl
  • link.click()
  • window.URL.revokeObjectURL(link.href)
  • } catch (e) {
  • alertDanger('Download failed!');
  • }
  • })
  • .catch(function (err) {
  • alertDanger('Download failed!');
  • });
  • },