【发布时间】:2017-06-08 13:36:52
【问题描述】:
我的网站摘要:用户填写一些信息,点击“提交”后,信息通过 AJAX 提交到后端。后端收到信息后,会使用该信息生成 DOCX,并将该 DOCX 文件提供给用户。
这是我的 HTML 文件中的 AJAX 代码
$.ajax({
type:'POST',
url:'/submit/',
data:{
data that I submit
},
dateType: 'json',
success:function() {
document.location = "/submit";
}
})
使用 send_file 返回文件的 /submit/ 的我的视图函数
def submit(request):
#Receive Data
#Create a File with the Data and save it to the server
return send_file(request)
def send_file(request):
lastName = get_last_name() +'.docx'
filename = get_full_path() # Select your file here.
wrapper = FileWrapper(open(filename , 'rb'))
response = HttpResponse(wrapper, content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
response['Content-Disposition'] = 'attachment; filename=' + lastName
response['Content-Length'] = os.path.getsize(filename)
return response
这已经完美地运行了一段时间了。但是,当我将托管帐户中的“网络工作者”/进程的数量从 1 个增加到 4 个时,我开始遇到问题。正在发生的事情是一个不同的网络工作者被用来发送文件,它正在创建一个新的站点实例来做到这一点。这样做的问题是新实例不包含使用创建文件的网络工作者创建的文件路径。
就像我说的,当我的 web 应用程序只有一个“网络工作者”或一个进程时,这可以完美地工作。现在我只有大约 50% 的成功率。
它几乎就像一个进程试图在文件创建之前发送它。或者该进程无权访问创建它的进程所拥有的文件名。
任何帮助将不胜感激。谢谢!
代码试图通过请求发送路径名,然后返回服务器。
提交视图返回文件信息回ajax。
def submit(request):
# Receive DATA
# Generate file with data
lastName = get_last_name() +'.docx'
filename = get_full_path() # Select your file here.
return HttpResponse(json.dumps({'lastname': lastName,'filename':filename}), content_type="application/json")
AJAX的成功函数
success:function(fileInfo) {
name_last = fileInfo['lastname']
filepath= fileInfo['filepath']
document.location = "/send";
}
那么我可以使用 "/send" 获取要发送的文件信息吗?
【问题讨论】:
-
请添加get_full_path(),需要调试。
标签: javascript jquery ajax django pythonanywhere