1
我怎样才能返回一个JSON响应和文件响应:返回JSON和文件
现在我这样做:
runNumber = "A0001"
response = None
try:
response = make_response("Line One\r\nLine Two\r\n")
response.headers["Content-Disposition"] = "attachment; filename=" + runNumber + ".txt"
except MyCustomException as e:
response = jsonify(error=e.value, runnumber=runNumber)
except:
raise
return(response)
但是,只允许我回JSON或文件。在某些情况下,我想要返回两者。
我想要返回JSON和文件的情况是当用户在使用该文件之前应该检查的文件内容的警告。
如果这是不可能的,我会将警告添加到文件的内容。
然后客户如何将文件和JSON部分再次分开? –
这在HTTP中是不可能的 - 你的响应有一个Content-Type(和一个Content-Disposition)。并且多部分响应根本不受浏览器的任何标准化方式的支持([1](http://stackoverflow.com/questions/10729733/is-there-a-de-facto-or-established-reason-why-multipart -http-responses-arent-ge),[2](http://stackoverflow.com/questions/1806228/browser-support-of-multipart-responses)) –