从django下载XLS文件下载代码时,文件未打开,它显示了损坏
这是我的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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FILERESPONSE
将有效,问题是您没有设置正确的关键字参数。确保您已将
'rb'
添加到Open
中,这应该对您有用。The
FileResponse
one will work, the issue is that you are not setting the proper keyword arguments.Make sure you have added the
'rb'
to theopen
and this should work for you.