Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用django发送文件_Django_Axios - Fatal编程技术网

使用django发送文件

使用django发送文件,django,axios,Django,Axios,我想从客户端(axios send)向服务器发送一个请求,服务器返回一个文件, 客户端将接受此文件,并使用javascript修改此文件, 然后在浏览器上显示文件数据 我认为views.pyresponse或axios中发生了一些错误 我为此奋斗了一天 而且,我不熟悉django。我只是从网络博客中学到一点 vscode命令显示此错误 Exception happened during processing of request from ('127.0.0.1', 9453) Tracebac

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

我认为
views.py
response或
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

view.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
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),
]

url.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
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)
      })