Shawn Lu
Shawn Lu

Reputation: 121

force-download with Django and Vue

I was trying use vue to download file from server

when I director visit my backends API, it work well,and I can unzip my file normal.

but when I try to download file by axios response, the file can not unzip,and also if I don't set link.download, the filename does not correct.like that enter image description here

here is my vue's api

export function packMaterials (context, thesisId, filename) {
  context.$axios({
    method: 'get',
    headers: getAuthHeader(),
    url: PACK_URL + thesisId
  })
    .then(function (response) {
      console.log(response)
      let blob = new Blob([response.data], { type: 'application/force-download' })
      let link = document.createElement('a')
      link.href = window.URL.createObjectURL(blob)
      link.download = filename
      link.click()
    })
    .catch(function (error) {
      console.log(error)
    })
}

here is my Django API

 if os.path.exists(file_path):
    # 返回file
    with open(file_path, 'rb') as fh:
        response = HttpResponse(fh.read(), content_type="application/force-download")
        response['Content-Disposition'] = "attachment; filename={}".format(escape_uri_path(filename))
        return response

and here is the response data in console. enter image description here

Thanks

Upvotes: 2

Views: 1590

Answers (0)

Related Questions