image-cross-domain-upload.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>图片跨域上传示例 - Editor.md examples</title>
  6. <link rel="stylesheet" href="css/style.css" />
  7. <link rel="stylesheet" href="../css/editormd.css" />
  8. <link rel="shortcut icon" href="https://pandao.github.io/editor.md/favicon.ico" type="image/x-icon" />
  9. </head>
  10. <body>
  11. <div id="layout" style="height: 2000px;background: #f6f6f6;">
  12. <header>
  13. <h1>图片跨域上传示例</h1>
  14. <p>Image cross-domain upload example.</p>
  15. </header>
  16. <div id="test-editormd">
  17. <textarea style="display:none;">#### Settings
  18. ```javascript
  19. {
  20. imageUpload : true,
  21. imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
  22. imageUploadURL : "http://xxxxxxx/editor.md/examples/php/cross-domain-upload.php?test=dfdf",
  23. crossDomainUpload : true,
  24. uploadCallbackURL : "http://xxxxxx/upload_callback.html?test=dfdf"
  25. }
  26. ```
  27. #### 跨域上传原理
  28. 利用 iframe 来实现,A 站 POST 到 B 站,B 站处理上传后再跳转回到 A 站的 callback 页面,callback 页面此时在 iframe 内(即同域下),再通过 window.parent 就可以操作父页面的任意元素了。
  29. #### 跨域上传示例 PHP 版
  30. 当前域名:blog.xxx.com/post.php
  31. ```html
  32. &lt;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"&gt;
  33. &lt;input type="file" name="file" accept="image/*" /&gt;
  34. &lt;input type="submit" id="submit" value="开始上传" /&gt;
  35. &lt;/form&gt;
  36. &lt;iframe name="upload-iframe" style="display:none;" frameborder="0"&gt;&lt;/iframe&gt;
  37. ```
  38. 图片服务器:static.xxx.com/uploader.php
  39. ```php
  40. &lt;?php
  41. header("Access-Control-Allow-Origin: *"); // Setting allow domian name
  42. $file = 'uploads/' . $_FILES['file']['name'];
  43. // 详细过程略
  44. move_uploaded_file($_FILES['file']['tmp_name'], $file);
  45. $location = $_GET['callback'].'?success=1&message=xxxxxxx&url=http://static.xxx.com/2015/02/15'.$file.'&temp='.date('ymdhis');
  46. header('location:' . $location);
  47. exit;
  48. ?&gt;
  49. ```
  50. 当前域名:blog.xxx.com/upload-callback.html
  51. ```html
  52. &lt;script type="text/javascript"&gt;
  53. var query = {};
  54. var urlParams = window.location.search.split('?')[1];
  55. urlParams = urlParams.split("&");
  56. for (var i = 0; i&lt; urlParams.length; i++) {
  57. var param = urlParams[i].split("=");
  58. query[param[0]] = param[1];
  59. }
  60. var imageDialog = window.parent.document.getElementById(query['dialog_id']);
  61. //console.log(imageDialog, window.parent.document, window.parent, query);
  62. &lt;/script&gt;
  63. ```</textarea>
  64. </div>
  65. </div>
  66. <script src="js/jquery.min.js"></script>
  67. <script src="../editormd.js"></script>
  68. <script type="text/javascript">
  69. $(function() {
  70. var testEditor = editormd("test-editormd", {
  71. width : "90%",
  72. height : 640,
  73. markdown : "",
  74. path : '../lib/',
  75. imageUpload : true,
  76. imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
  77. imageUploadURL : "http://www.ipandao.com/editor.md/examples/php/cross-domain-upload.php?test=dfdf",
  78. crossDomainUpload : true,
  79. uploadCallbackURL : "http://localhost/editor.md/examples/php/upload_callback.html?test=dfdf"
  80. /*
  81. 跨域时,上传的图片服务器后台只需要返回一个跳转 URL 并跳转到原页面同域下的 callback 页面,结构如下:
  82. {
  83. success : 0 | 1, // 0 表示上传失败,1 表示上传成功
  84. message : "提示的信息,上传成功或上传失败及错误信息等。",
  85. dialog_id : $_GET['dialog_id'],
  86. url : "远程图片地址" // 上传成功时才返回
  87. }
  88. */
  89. });
  90. });
  91. </script>
  92. </body>
  93. </html>