简体   繁体   English

在流文件或来自ifram的HTTP标头之前返回JSON?

[英]Return JSON before streaming file or HTTP headers from ifram?

I have a form that uses a hidden iframe to submit a file to a script that changes the file and then returns the changed file. 我有一个使用隐藏的iframe将文件提交到脚本的表单,该脚本会更改文件,然后返回更改后的文件。 I found that I don't actually have to save the file anywhere if I just do something along the lines of echo file_get_contents(tmp); 我发现,如果我只是按照echo file_get_contents(tmp);执行一些操作,实际上并不需要将文件保存在任何地方echo file_get_contents(tmp); , where tmp is the path of the file uploaded to the apache tmp directory. ,其中tmp是上传到apache tmp目录的文件的路径。

The script also does: header("Content-type: application/octet-stream") so that when the iframe loads, the user is prompted for download. 该脚本还执行以下操作: header("Content-type: application/octet-stream")以便在加载iframe时,提示用户进行下载。 I would like to know, on the client side, if everything went alright with the server. 我想知道,在客户端,服务器是否一切正常。 It's not an XMLHttpRequest, so I can't check the headers, and the only thing returned is the file itself. 它不是XMLHttpRequest,所以我无法检查标题,唯一返回的是文件本身。

Is there some way to return some json before streaming the file? 有什么方法可以在流式传输文件之前返回一些json吗? Or, is there a way to check the headers of an iframe? 或者,有没有办法检查iframe的标头?

Currently, I've been setting a cookie with the server and checking every half a second with javascript to see if the cookie was set. 目前,我一直在服务器上设置cookie,并使用javascript每隔半秒检查一次cookie是否已设置。 I would prefer a less hacky solution than this though. 不过,我宁愿选择一个比这个更简单的解决方案。

If you stream JSON before the file, the MIME header will be invalid and the browser will not be able to download the file. 如果在文件之前流JSON,则MIME标头将无效,浏览器将无法下载该文件。

In terms of checking "if everything went alright with the server", why wouldn't you be determining this on the server , rather than going round-trip to the client and back again? 就检查“服务器是否一切正常”而言,您为什么不在服务器上确定这一点,而不是往返于客户端并再次返回? Allowing it to error-out on the client smells of bad design. 允许它在客户端上出错,这是不良设计的味道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM