图片跨域上传示例

Image cross-domain upload example.

x
1
#### Settings
2
3
```javascript
4
{
5
    imageUpload       : true,
6
    imageFormats      : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
7
    imageUploadURL    : "http://xxxxxxx/editor.md/examples/php/cross-domain-upload.php?test=dfdf",
8
    crossDomainUpload : true,
9
    uploadCallbackURL : "http://xxxxxx/upload_callback.html?test=dfdf"
10
}
11
```
12
13
#### 跨域上传原理
14
15
利用 iframe 来实现,A 站 POST 到 B 站,B 站处理上传后再跳转回到 A 站的 callback 页面,callback 页面此时在 iframe 内(即同域下),再通过 window.parent 就可以操作父页面的任意元素了。
16
17
#### 跨域上传示例 PHP 版
18
19
当前域名:blog.xxx.com/post.php
20
21
```html
22
<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">
23
    <input type="file" name="file" accept="image/*" />
24
    <input type="submit" id="submit" value="开始上传" />
25
</form>
26
<iframe name="upload-iframe" style="display:none;" frameborder="0"></iframe>  
27
```     
28
29
图片服务器:static.xxx.com/uploader.php
30
31
```php
32
<?php
33
header("Access-Control-Allow-Origin: *"); // Setting allow domian name
34
35
$file = 'uploads/' . $_FILES['file']['name'];
36
// 详细过程略
37
move_uploaded_file($_FILES['file']['tmp_name'], $file);
38
39
$location = $_GET['callback'].'?success=1&message=xxxxxxx&url=http://static.xxx.com/2015/02/15'.$file.'&temp='.date('ymdhis');
40
41
header('location:' . $location);
42
exit;
43
?>
44
```
45
46
当前域名:blog.xxx.com/upload-callback.html
47
48
```html
49
<script type="text/javascript"> 
50
    var query = {};
51
    var urlParams = window.location.search.split('?')[1];
52
    urlParams = urlParams.split("&");
53
54
    for (var i = 0; i< urlParams.length; i++) {
55
        var param = urlParams[i].split("="); 
56
        query[param[0]] = param[1]; 
57
    }
58
59
    var imageDialog = window.parent.document.getElementById(query['dialog_id']);
60
    //console.log(imageDialog, window.parent.document, window.parent, query);
61
</script>
62
```

Settings

  1. {
  2. imageUpload : true,
  3. imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
  4. imageUploadURL : "http://xxxxxxx/editor.md/examples/php/cross-domain-upload.php?test=dfdf",
  5. crossDomainUpload : true,
  6. uploadCallbackURL : "http://xxxxxx/upload_callback.html?test=dfdf"
  7. }

跨域上传原理

利用 iframe 来实现,A 站 POST 到 B 站,B 站处理上传后再跳转回到 A 站的 callback 页面,callback 页面此时在 iframe 内(即同域下),再通过 window.parent 就可以操作父页面的任意元素了。

跨域上传示例 PHP 版

当前域名:blog.xxx.com/post.php

  1. <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">
  2. <input type="file" name="file" accept="image/*" />
  3. <input type="submit" id="submit" value="开始上传" />
  4. </form>
  5. <iframe name="upload-iframe" style="display:none;" frameborder="0"></iframe>

图片服务器:static.xxx.com/uploader.php

  1. <?php
  2. header("Access-Control-Allow-Origin: *"); // Setting allow domian name
  3. $file = 'uploads/' . $_FILES['file']['name'];
  4. // 详细过程略
  5. move_uploaded_file($_FILES['file']['tmp_name'], $file);
  6. $location = $_GET['callback'].'?success=1&message=xxxxxxx&url=http://static.xxx.com/2015/02/15'.$file.'&temp='.date('ymdhis');
  7. header('location:' . $location);
  8. exit;
  9. ?>

当前域名:blog.xxx.com/upload-callback.html

  1. <script type="text/javascript">
  2. var query = {};
  3. var urlParams = window.location.search.split('?')[1];
  4. urlParams = urlParams.split("&");
  5. for (var i = 0; i< urlParams.length; i++) {
  6. var param = urlParams[i].split("=");
  7. query[param[0]] = param[1];
  8. }
  9. var imageDialog = window.parent.document.getElementById(query['dialog_id']);
  10. //console.log(imageDialog, window.parent.document, window.parent, query);
  11. </script>