File upload 跨域上传
有没有办法使用url a中的表单在其“action”属性中指向url B而不将页面重定向到url B来上载文件 我知道是3年后的事了,但今天我自己也遇到了同样的问题,我想我应该把我的解决方案发布在这里。它不是最漂亮的,但它使用了iframe,这应该有助于传统浏览器更好地支持它。iframe最大的挑战是,如果iframe位于另一个域上,出于安全原因(浏览器限制了上述功能),则无法读取其内容 本质上,我的解决方案是:File upload 跨域上传,file-upload,cross-domain,File Upload,Cross Domain,有没有办法使用url a中的表单在其“action”属性中指向url B而不将页面重定向到url B来上载文件 我知道是3年后的事了,但今天我自己也遇到了同样的问题,我想我应该把我的解决方案发布在这里。它不是最漂亮的,但它使用了iframe,这应该有助于传统浏览器更好地支持它。iframe最大的挑战是,如果iframe位于另一个域上,出于安全原因(浏览器限制了上述功能),则无法读取其内容 本质上,我的解决方案是: 用户选择要上载的文件 封装文件字段的表单发送一个POST请求,目标是隐藏的ifra
位置
重定向,将iframe发送回HTTP\u REFERER
,它将与表单页面位于同一域中,这意味着脚本将能够读取内容<form method="post" action="http://other/website/upload.php" enctype="multipart/form-data">
<input type="file" name="file" />
</form>
<br /><br />
<img id="avatar" />
$('form').submit(function () {
$('<iframe></iframe>').attr({name: 'iframe', id: 'iframe'}).prependTo($('body')).load(function () {
var response = JSON.parse(decodeURIComponent(/[\\?&]_response=([^&#]*)/.exec(this.contentWindow.location.href)[1]).replace(/\+/g, ' '));
if (response.error) {
alert(response.error);
} else {
$('#avatar').attr('src', response.url);
}
$(this).remove();
});
$(this).attr('target', 'iframe');
});
$('input[name="avatar"]').change(function () {
$('form').submit();
});
PHP:
<form method="post" action="http://other/website/upload.php" enctype="multipart/form-data">
<input type="file" name="file" />
</form>
<br /><br />
<img id="avatar" />
$('form').submit(function () {
$('<iframe></iframe>').attr({name: 'iframe', id: 'iframe'}).prependTo($('body')).load(function () {
var response = JSON.parse(decodeURIComponent(/[\\?&]_response=([^&#]*)/.exec(this.contentWindow.location.href)[1]).replace(/\+/g, ' '));
if (response.error) {
alert(response.error);
} else {
$('#avatar').attr('src', response.url);
}
$(this).remove();
});
$(this).attr('target', 'iframe');
});
$('input[name="avatar"]').change(function () {
$('form').submit();
});
您的问题有点不清楚。您能否更明确地说明“重定向”的含义?通常,当您提交表单时,它会重定向到“操作”中定义的url。我在问是否有办法避免这种情况发生。我知道这可以通过Javascript/Ajax、Iframes等实现,但这些解决方案存在一些跨域问题。我只是想听听你们的想法。所以你们想要一个不涉及脚本的解决方案?只是简单的HTML?那么就不需要了。你需要AJAX来发回表单,而不需要发回整个页面。我对这些解决方案持开放态度。我唯一的限制是没有flash/swfupload。可能是