为什么我的 Django 视图没有自动下载文件

Why is my Django view not automatically downloading a file

提问人:tthheemmaannii 提问时间:10/9/2023 更新时间:10/9/2023 访问量:27

问:

我的视图转录文件并输出 SRT 脚本。然后它找到并应该自动下载 T 成绩单,但在转录完成后没有任何反应(文件是在使用 transcribeSubmit 视图的上一页上提交的。处理和返回文件的视图是initiate_transcription视图)。这是我 views.py:

@csrf_protect
def transcribeSubmit(request):
    
    if request.method == 'POST':
        
        
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            

            uploaded_file = request.FILES['file']
            fs = FileSystemStorage()
            filename = fs.save(uploaded_file.name, uploaded_file)
            request.session['uploaded_file_name'] = filename
            request.session['uploaded_file_path'] = fs.path(filename)
            
            
            #transcribe_file(uploaded_file)
            #return redirect(reverse('proofreadFile'))
            return render(request, 'transcribe/transcribe-complete.html', {"form": form})
        else:
            
    else:
        form = UploadFileForm()

        
        
    return render(request, 'transcribe/transcribe.html', {"form": form})

@csrf_protect
def initiate_transcription(request):
    
    if request.method == 'POST':
        
        
        # get the file's name and path from the session
        file_name = request.session.get('uploaded_file_name')
        file_path = request.session.get('uploaded_file_path')
        if file_name and file_path:
            with open(file_path, 'rb') as f:
                path_string = f.name
                transcribe_file(path_string)
            
            file_extension = ('.' + (str(file_name).split('.')[-1]))
            transcript_name = file_name.replace(file_extension, '.srt')
            
            transcript_path = file_path.replace((str(file_path).split('\\')[-1]), transcript_name)
                
            file_location = transcript_path
            with open(file_location, 'r') as f:
                file_data = f.read()

            response = HttpResponse(file_data, content_type='text/plain')
            response['Content-Disposition'] = 'attachment; filename="' + transcript_name + '"'
            return response
            
        else:
            #complete

    return render(request, 'transcribe/transcribe-complete.html', {"form": form})

           
def transcribe_file(path):
#transcription logic

WSG公司

const form = document.querySelector('form');
form.addEventListener('submit', function (event) {
    event.preventDefault();

    const formData = new FormData(form);
    const xhr = new XMLHttpRequest();

    xhr.open('POST', form.getAttribute('action'), true);
    xhr.onreadystatechange = function () {
        console.log("ready state: ", xhr.readyState);
        console.log("status: ", xhr.status);
    }
    xhr.responseType = 'blob';
    xhr.send(formData);
});

HTML格式:

<form id="initiate-transcription-form" method="post" action="{% url 'initiate_transcription' %}" enctype="multipart/form-data">
                    {% csrf_token %}
                <div>
                    <label for="output-lang--select-summary"></label>
                    <select class="output-lang-select" id="output-lang--select-summary" name="audio_language">
                        <option value="detect">Detect language</option>
                        <option value="zh">Chinese</option>
                        <option value="en">English</option>
                        <option value="es">Spanish</option>
                        <option value="de">German</option>
                        <option value="jp">Japanese</option>
                        <option value="ko">Korean</option>
                        <option value="ru">Russian</option>
                        <option value="pt">Portugese</option>
                <!-- add more language options as needed -->
                    </select>
                </div> 
                <button id="transcribe-button" type="submit">Click to Transcribe
                        <svg class="download-transcript" width="18" height="18" viewBox="0 0 24 24">
                            <path d="M19,9h-4V3H9v6H5l7,7L19,9z M5,18v2h14v-2H5z" fill="white"/>
                        </svg>
                            
                    </button>   
                
    </form>

我得到的唯一响应是状态:JS中的200控制台日志。

JavaScript Python HTML Django 文件

评论

0赞 Mohamed ElKalioby 10/9/2023
检查file_path和file_name是否在会话中都有价值
0赞 tthheemmaannii 10/10/2023
@MohamedElKalioby 嗨。它们都具有有效值。

答: 暂无答案