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)) –