跨域上传
有没有办法使用 url A 中的表单上传文件,该表单在其“action”属性中指向 url B,而不将页面重定向到 url B?
Are there ways to upload files using a form in url A that points to url B in its "action" attribute without redirecting the page to url B?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道已经是三年后的事了,但由于我今天也有同样的问题,所以我想我应该在这里发布我的解决方案。它不是最漂亮的,但它使用 iframe,这应该有助于它得到旧版浏览器的更多支持。 iframe 的最大挑战是,出于安全原因,如果 iframe 位于另一个域中,您将无法读取 iframe 的内容(浏览器限制所述功能)。
本质上,我的解决方案是这样的:
Location
重定向,将 iframe 发送回HTTP_REFERER
,这将与表单页面位于同一域中,这意味着脚本将能够读取内容。HTML:
JavaScript:
PHP:
I know it's 3 years later, but since I had this same question myself today, I figured I'd post my solution here. It's not the prettiest, but it uses an iframe, which should help it be more supported by legacy browsers. The biggest challenge of the iframe is that you can't read the contents of an iframe if it's on another domain for security reasons (the browser restricts said functionality).
In essence, my solution is this:
Location
redirect, sending the iframe back toHTTP_REFERER
, which will be the on the same domain as the form page, which means that the script will be able to read the contents.The HTML:
The JavaScript:
The PHP: