gpt4 book ai didi

python - Django - 通过 ajax 请求提供文件

转载 作者:行者123 更新时间:2023-12-04 18:06:51 24 4
gpt4 key购买 nike

我有一个 extjs ajax 函数,它向服务器发送一个表单,该服务器返回一个文件以使用正确的标题下载。可以使用浏览器的正常文件下载窗口下载文件。但是ajax请求的成功回调函数并没有被触发,ajax一直挂起等待响应。

有没有办法告诉 ajax 函数文件是否从服务器正确发送?

  exportLayer = function(node_id){
Ext.Ajax.request({
method: "GET",
form: "exportForm",
url: "/basqui/layer/shapefile/export/" + node_id + "/",
success: function(r){
html = Ext.decode(r.responseText).html
Ext.get('pageContent').update(html);
},
});
}

服务器端:

    temp = tempfile.TemporaryFile()
zip = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
shapefileBase = os.path.splitext(dstFile)[0]
shapefileName = os.path.splitext(layer.filename)[0]
for fName in os.listdir(dstDir):
zip.write(os.path.join(dstDir, fName), fName)
zip.close()

#delete temporary files
shutil.rmtree(dstDir)

#return the zip to user
f = FileWrapper(temp)
response = StreamingHttpResponse(f, content_type="application/zip")
response['Content-Disposition'] = "attachment; filename=" + shapefileName + fileExt_dic[format]
response['Content-Length'] = temp.tell()
temp.seek(0)

return response

最佳答案

您不能使用 Jquery Ajax 下载。响应不会到达浏览器。我曾经遇到过这个问题,我通过这样做解决了它:

$(".dwnBtn").click(function(){
let url = "{% url 'the url'%}"
$.post(url, function(data)
{
console.log(data);
location.replace(url);
});

})这将使浏览器重新加载,然后下载您的文件。

关于python - Django - 通过 ajax 请求提供文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24579109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com