我需要从 API 获取文件,然后将其保存在调查对象中。它似乎工作正常,即我可以获取文件并将其保存在我需要的地方,但是会引发 ConnectionAbortedError 并且文件会上传两次。保存在模型中的那个是最后一个。
- 这是
views.py
:
def send_survey(request):
## Using convertApi to merge PDFs
convertapi.api_secret = settings.CONVERTAPI_SECRET
result = convertapi.convert('merge', {'Files': [files_url_list]})
## Getting the file from the url
r = requests.get(result.file.url)
## Saving the file to the Survey object
if r.status_code == 200:
contentFile = ContentFile(r.content)
s = Survey()
s.file.save('Last survey', contentFile, save=True)
messages.success(request, "The literature survey saved successfully!")
else:
messages.error(request, r.status_code)
return redirect('homepage')
- 和
models.py
:
class Survey(models.Model):
date = models.DateField('Date', default=date.today)
file = models.FileField('Survey', upload_to='literature_survey/previous_surveys', default=None)
sent = models.BooleanField('Sent?', default=False)
def __str__(self):
return f"Survey #{self.pk}"
我收到以下错误:
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 62615)
Traceback (most recent call last):
File "C:\Python\Python38\lib\socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "C:\Python\Python38\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python\Python38\lib\socketserver.py", line 720, in __init__
self.handle()
File "C:\Python\Python38\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle
self.handle_one_request()
File "C:\Python\Python38\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "C:\Python\Python38\lib\socket.py", line 669, in readinto
return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
----------------------------------------