图片跨域上传示例
Image cross-domain upload example.
#### Settings ```javascript { imageUpload : true, imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"], imageUploadURL : "http://xxxxxxx/editor.md/examples/php/cross-domain-upload.php?test=dfdf", crossDomainUpload : true, uploadCallbackURL : "http://xxxxxx/upload_callback.html?test=dfdf" } ``` #### 跨域上传原理 利用 iframe 来实现,A 站 POST 到 B 站,B 站处理上传后再跳转回到 A 站的 callback 页面,callback 页面此时在 iframe 内(即同域下),再通过 window.parent 就可以操作父页面的任意元素了。 #### 跨域上传示例 PHP 版 当前域名:blog.xxx.com/post.php ```html <form method="post" target="upload-iframe" enctype="multipart/form-data" action="http://static.xxx.com/uploader.php?callback=http://blog.xxx.com/upload-callback.html"> <input type="file" name="file" accept="image/*" /> <input type="submit" id="submit" value="开始上传" /> </form> <iframe name="upload-iframe" style="display:none;" frameborder="0"></iframe> ``` 图片服务器:static.xxx.com/uploader.php ```php <?php header("Access-Control-Allow-Origin: *"); // Setting allow domian name $file = 'uploads/' . $_FILES['file']['name']; // 详细过程略 move_uploaded_file($_FILES['file']['tmp_name'], $file); $location = $_GET['callback'].'?success=1&message=xxxxxxx&url=http://static.xxx.com/2015/02/15'.$file.'&temp='.date('ymdhis'); header('location:' . $location); exit; ?> ``` 当前域名:blog.xxx.com/upload-callback.html ```html <script type="text/javascript"> var query = {}; var urlParams = window.location.search.split('?')[1]; urlParams = urlParams.split("&"); for (var i = 0; i< urlParams.length; i++) { var param = urlParams[i].split("="); query[param[0]] = param[1]; } var imageDialog = window.parent.document.getElementById(query['dialog_id']); //console.log(imageDialog, window.parent.document, window.parent, query); </script> ```