首页 > 解决方案 > 使用 django 发送文件

问题描述

我想从客户端(axios发送)发送一个请求到服务器,服务器返回一个文件,客户端将接受这个文件,并使用javascript修改这个文件,然后在浏览器上显示文件数据

我认为views.py响应中发生了一些错误,或者axios得到了错误

我为此挣扎了一天。

而且,我不熟悉 django。我只是从网络博客中学到了一点。

vscode 命令显示此错误

Exception happened during processing of request from ('127.0.0.1', 9453)
Traceback (most recent call last):
  File "C:\Anaconda3\lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "C:\Anaconda3\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\lib\wsgiref\handlers.py", line 266, in write
    "write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\lib\wsgiref\handlers.py", line 141, in run
    self.handle_error()
  File "C:\lib\site-packages\django\core\servers\basehttp.py", line 123, in handle_error
    super().handle_error()
  File "C:\Anaconda3\lib\wsgiref\handlers.py", line 368, in handle_error
    self.finish_response()
  File "C:\Anaconda3\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Anaconda3\lib\wsgiref\handlers.py", line 274, in write
    self.send_headers()
  File "C:\Anaconda3\lib\wsgiref\handlers.py", line 331, in send_headers
    if not self.origin_server or self.client_is_modern():
  File "C:\Anaconda3\lib\wsgiref\handlers.py", line 344, in client_is_modern
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Anaconda3\lib\socketserver.py", line 639, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\\Anaconda3\lib\socketserver.py", line 361, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Anaconda3\lib\socketserver.py", line 696, in __init__
    self.handle()
  File "C:\Anaconda3\lib\site-packages\django\core\servers\basehttp.py", line 176, in handle
    self.handle_one_request()
  File "C:\Anaconda3\lib\site-packages\django\core\servers\basehttp.py", line 201, in handle_one_request
    handler.run(self.server.get_app())
  File "C:\Anaconda3\lib\wsgiref\handlers.py", line 144, in run
    self.close()
  File "C:\Anaconda3\lib\site-packages\django\core\servers\basehttp.py", line 118, in close
    super().close()
  File "C:\Anaconda3\lib\wsgiref\simple_server.py", line 35, in close
    self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
--------------------------------------------------------------------------------------
[02/May/2021 00:00:00] "POST /about HTTP/1.1" 200 0
Traceback (most recent call last):
  File "C:\Anaconda3\lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "C:\Anaconda3\lib\wsgiref\handlers.py", line 180, in finish_response
    self.write(data)
  File "C:\Anaconda3\lib\wsgiref\handlers.py", line 266, in write
    "write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
[02/May/2021 00:00:00] "POST /about HTTP/1.1" 500 59

视图.py

from django.views.decorators.csrf import csrf_exempt
from django.http import FileResponse

@csrf_exempt
def sendFile(request):
  
    response = FileResponse(open(r'C:/test.txt'))   

    return response

网址.py

from django.contrib import admin
from django.urls import path

from musics.views import hello_view
from musics.views import sendFile

urlpatterns = [
    path('about', sendFile),
]

axios

axios
     .post('http://127.0.0.1:8000/about', { name: 'mary', }, { responseType: 'blob' })
      .then((response) => {
           console.log(response)
      })

标签: djangoaxios

解决方案


推荐阅读