从django下载XLS文件下载代码时,文件未打开,它显示了损坏

发布于 2025-02-10 23:49:19 字数 1898 浏览 0 评论 0原文

这是我的XLS代码使用Python(Django)

Views.pys.py

def downloadfile(request, filename=''):
  if filename != '':
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    filename = 'SampleExcel.xls'
    filepath = BASE_DIR + "\SampleExcel\SampleExcel.xls"
    path = open(filepath, encoding='cp437')
    mime_type, _ = mimetypes.guess_type(filepath)
    response = HttpResponse(path, content_type=mime_type)
    response['Content-Disposition'] = "attachment; filename=%s" % filename
    return response
  else:
    return HttpResponseRedirect("adduser", {})

使用此代码,我可以正确下载文件,但是在打开该文件时,它会显示出错误。我附加了错误的屏幕截图。

我尝试了此解决方案的代码,

if filename != '':
  BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  filename = 'SampleExcel.xls'
  filepath = BASE_DIR + "\SampleExcel\SampleExcel.xls" 
  return FileResponse(open(filepath, 'rb') , True, filename)
else:
  return HttpResponseRedirect("adduser", {})

这也不有效,它在/downloadfile/samplesexcel.xls上显示错误'typeerror HTTP状态代码必须是整数。”,甚至没有下载。

请帮我。从过去的三天开始,我被困在这里。

注意:我只需要下载XLS文件,因为下载后,我必须使用该下载的XLS文件执行导入功能。我无法将XLS更改为XLSX。

错误日志

<代码>追溯(最新呼叫最后):文件“ c:\ users \ pcsi \ appdata \ local \ local \ program \ python \ python \ python310 \ lib \ lib \ lib \ wsgiref \ wsgiref \ handlers。 py”,第138行,在运行self.finish_response()文件中“ c:\ users \ users \ pcsi \ appdata \ local \ program \ python \ python \ python \ python310 \ lib \ lib \ wsgiref \ wsgiref \ handlers.py”数据)文件“ c:\ user \ pcsi \ appdata \ local \ program \ python \ python \ python310 \ lib \ wsgiref \ wsgiref \ handlers.py”,第279行,在写入assert类型(数据)中,必须是一个字节实例[27/jun/2022 15:07:10]”

Here is my code for xls download using python(Django)

views.py

def downloadfile(request, filename=''):
  if filename != '':
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    filename = 'SampleExcel.xls'
    filepath = BASE_DIR + "\SampleExcel\SampleExcel.xls"
    path = open(filepath, encoding='cp437')
    mime_type, _ = mimetypes.guess_type(filepath)
    response = HttpResponse(path, content_type=mime_type)
    response['Content-Disposition'] = "attachment; filename=%s" % filename
    return response
  else:
    return HttpResponseRedirect("adduser", {})

With this code I am able to download file properly, but while opening that file it shows me error. I attach screenshot of error.

enter image description here

I have tried this code for solution,

if filename != '':
  BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  filename = 'SampleExcel.xls'
  filepath = BASE_DIR + "\SampleExcel\SampleExcel.xls" 
  return FileResponse(open(filepath, 'rb') , True, filename)
else:
  return HttpResponseRedirect("adduser", {})

This also not works, It shows error 'TypeError at /downloadfile/SampleExcel.xls
HTTP status code must be an integer.', and it is not even downloading.

Please help me. I am stuck here from last 3 days.

Note: I need to download xls file only, as after downloading it I have to perform import functionality with that downloaded xls file. I can not change xls to xlsx.

Error Log

Traceback (most recent call last): File "C:\Users\PCSI\AppData\Local\Programs\Python\Python310\lib\wsgiref\handlers.py", line 138, in run self.finish_response() File "C:\Users\PCSI\AppData\Local\Programs\Python\Python310\lib\wsgiref\handlers.py", line 184, in finish_response self.write(data) File "C:\Users\PCSI\AppData\Local\Programs\Python\Python310\lib\wsgiref\handlers.py", line 279, in write assert type(data) is bytes, \ AssertionError: write() argument must be a bytes instance [27/Jun/2022 15:07:10] "GET /downloadfile/SampleExcel.xls HTTP/1.1" 500 59

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

貪欢 2025-02-17 23:49:19

FILERESPONSE将有效,问题是您没有设置正确的关键字参数。

path = open(filepath, 'rb')
response = FileResponse(path, as_attachment=True, filename=filename)

确保您已将'rb'添加到Open中,这应该对您有用。

The FileResponse one will work, the issue is that you are not setting the proper keyword arguments.

path = open(filepath, 'rb')
response = FileResponse(path, as_attachment=True, filename=filename)

Make sure you have added the 'rb' to the open and this should work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文