首页 > php代码 > 阿里云OSS利用iframe实现图片异步上传

阿里云OSS利用iframe实现图片异步上传

阿里云OSS上传工具是阿里云专用的上传功能了,下面我就为各位介绍一个阿里云OSS利用iframe实现图片异步上传了,这个例子非常的简单。

阿里云 OSS 图片上传iframe 实现异步上传 相关代码:

index.php:

<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
if (isset($_POST) && !empty($_POST)) {
    echo "<pre>";
    var_dump($_POST);
    die;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="http://filela.b0.upaiyun.com/js/jquery-1.9.0.min.js"></script>
    </head>
    <body>
        <h1>添加操作</h1>
        <form action="" method="post" >
            <iframe src="upload.php"  frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe><br/>
            名称: <input type="text" value="" name="name">
            <div id="res"> </div>
            <input type="submit" value="submit" />
        </form>
    </body>
</html>

upload.php

<?php
$access_id = '';
$access_key = '';
$policy = '{"expiration": "2120-01-01T12:00:00.000Z","conditions":[{"bucket": "ioutsider" },["content-length-range", 0, 104857600]]}';
$policy = base64_encode($policy);
$signature = base64_encode(hash_hmac('sha1', $policy, $access_key, true));
$file_name = date('Y') . '/' . date('m') . '/' . md5(microtime(true)) . '.jpg';
//print_r(get_headers('http://ioutsider.oss-cn-shenzhen.aliyuncs.com')); 
?>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="http://filela.b0.upaiyun.com/js/jquery-1.9.0.min.js"></script>
    </head>
    <body>
        <form id="upload" action="http://ioutsider.oss-cn-shenzhen.aliyuncs.com" method="POST" enctype="multipart/form-data">
            <label for="file">选择文件:</label>
            <input type="hidden" name="OSSAccessKeyId" id="OSSAccessKeyId"  value="<?php echo $access_id; ?>" />
            <input type="hidden" name="policy" id="policy"  value='<?php echo $policy; ?>' />
            <input type="hidden" name="Signature" id="Signature"  value="<?php echo $signature; ?>" />
            <!--<input type="hidden" name="key" id="key"  value="upload/${filename}" />-->
            <input type="hidden" name="key" id="key"  value="<?php echo $file_name; ?>" />
            <input type="hidden" name="success_action_redirect" value="http://iphotos.me/res.php?img=<?php echo $file_name; ?>" />
            <input type="file" name="file" id="file" />
            <input type="submit" name="upload" value="上传" />
        </form>
    </body>
</html>

上传成功后跳转的页面:

<!DOCTYPE html>
<html>
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="http://filela.b0.upaiyun.com/js/jquery-1.9.0.min.js"></script>
    </head>
    <body>
        <?php $img = $_GET['img']; ?>
        <?php if ($img): ?>
            <img src='http://ioutsider.oss-cn-shenzhen.aliyuncs.com/<?php echo $img; ?>' width='200' height='200' />
            <script>
                $(function(){
                    alert('上传成功');
                    window.parent.$('#res').append("<input type='hidden' value='<?php echo $img ?>' name='path'>")
                })
            </script>
        <?php endif; ?>
    </body>
</html>


本文地址:http://www.phprm.com/code/73717.html

转载随意,但请附上文章地址:-)

标签:iframe

相关文章

发表留言