简体   繁体   English

如果刷新/重新打开页面,Django POST响应将引发异常

[英]Django POST Response throws exception if page is refreshed/reopened

I'm using Django in order to create a server which will download and track the progress of files. 我使用Django来创建服务器,该服务器将下载并跟踪文件的进度。 It has an input field for the url of the download link, and once I click on Download , it executes the following javascript code: 它具有用于下载链接的URL的输入字段,一旦单击Download ,它将执行以下javascript代码:

index.html index.html

function startDownload() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            completedFile = xhttp.responseText;
            document.getElementById(completedFile + "ProgBar").style.width = "100%";
            document.getElementById(completedFile + "ProgBar").innerHTML = "Completed!";
            setTimeout(delProgBar, 5000, completedFile);
        }
    };
    xhttp.open("POST", "http://localhost:8000/download/start/", true);
    xhttp.setRequestHeader("X-CSRFToken", '{{ csrf_token }}');
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    var downloadLink = document.getElementById("downloadInput").value;
    var sendString = "url=" + downloadLink;
    var downloadArray = downloadLink.split('/');
    var downloadName = downloadArray[downloadArray.length-1];
    addProgBar(downloadName, "0");
    xhttp.send(sendString);
}

Django views.py Django views.py

def start(request):
    aux = request.POST['url']
    file_name = start_download(aux)
    print("###########")
    print(file_name)
    return HttpResponse(file_name)

This works completely fine IF I don't reload the page after starting the download. 如果开始下载后我不重新加载页面,则此方法完全正常。 The POST Request is only being logged on Django Command Prompt window after it is completed (readyState check). POST请求仅在完成(readyState检查)后才在Django命令提示符窗口中记录。

However, the page should show the download progress for other people that open the page after the download started. 但是,页面应显示下载开始后其他打开页面的人的下载进度。 This however is throwing a big error on Django, with something like 4 Exceptions. 但是,这在Django上引发了一个大错误,类似4 Exceptions。 I can post it later if necessary, but the main one seems to be: 如有必要,我可以稍后发布,但是主要的似乎是:
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine ConnectionAbortedError:[WinError 10053]建立的连接被主机中的软件中止了

I believe that what I'm doing wrong is trying to send a response to a client that did not generated the request. 我认为我做错的是尝试向未生成请求的客户端发送响应。
Is there a way to receive the POST Response even if the client did not sent the original Request? 即使客户端没有发送原始请求,是否也可以接收POST响应? Or is there another way to do what I'm trying to? 还是有另一种方法可以做我想做的事?

Thanks a lot! 非常感谢!

Additional Info: 附加信息:
Tried with GET instead of POST and the same issue happens. 尝试使用GET而不是POST并发生相同的问题。

I believe that the main issue here is that the POST Request is only returning an answer to the client once the operation "start_download" finishes, which can take a while. 我认为这里的主要问题是,一旦“ start_download”操作完成,POST请求只会向客户端返回答案,这可能需要一段时间。 I should instead send a response with "success" or "fail" to start the download and use GET Request to perform polling and get the download status. 相反,我应该发送“成功”或“失败”的响应以开始下载,并使用GET Request进行轮询并获取下载状态。

Firstly it looks like the code checks for POST values. 首先,它看起来像代码检查POST值。 To fix this check if the request method is POST or GET. 要解决此问题,请检查请求方法是POST还是GET。 If its GET then you need to render to a page that displays the progress of the files. 如果是GET,则需要渲染到显示文件进度的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM