我的网站摘要:用户填写了一些信息,点击“提交”后,信息将通过AJAX提交到后端。在后端接收到信息后,它使用该信息生成DOCX,并将该DOCX文件返回给用户。
下面是我的HTML文件中的AJAX代码
$.ajax({
type:'POST',
url:'/submit/',
data:{
data that I submit
},
dateType: 'json',
success:function() {
document.location = "/submit";
}
})My Views函数用于使用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这已经完美地工作了一段时间了。然而,当我在我的托管账户中将“网络工作者”/processes的数量从1增加到4时,我开始遇到问题。发生的事情是,另一个web-worker正在被用来发送文件,它正在创建一个新的站点实例来执行此操作。这样做的问题是,新实例不包含使用创建文件的web worker创建的文件路径。
就像我说过的,当我的webApp只有一个"web worker“或一个进程时,它就能完美地工作。现在我只有大约50%的成功率。
这几乎就像是一个进程试图在文件创建之前发送它。或者该进程无权访问创建它的进程所拥有的文件名。
任何帮助都将不胜感激。谢谢!
尝试通过请求发送path_name然后返回到服务器的代码。
提交视图返回文件信息给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";
}那么我可以让fileINfo和"/send“一起发送吗?
发布于 2017-01-24 19:14:46
每个web worker都是一个独立的进程。他们没有访问在另一个worker中设置的变量的权限。每个请求都可以发送给任何worker,因此不能保证您使用的是为特定用户设置的文件名。如果您需要在请求之间传输信息,则需要将其存储在工作程序的内存之外--您可以将其存储在cookie中,或者在数据库或文件中。
https://stackoverflow.com/questions/41816589
复制相似问题