• [javascript]multipart/form-data上传格式表单自定义创建


    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    <input type="file" id="file">
    <button id="trigger">trigger</button>
    <script type="text/javascript">
        var file = document.getElementById("file");
        var trigger = document.getElementById("trigger");
    
        trigger.addEventListener("click", function(e){
            var reader = new FileReader();
            reader.readAsDataURL(file.files[0].slice());
            reader.addEventListener("loadend", function(e){
                req = new XMLHttpRequest();
                req.overrideMimeType("text/plain");
                req.addEventListener("load", function(e){
                    var img = document.createElement("img");
                    img.src = e.target.response;
                    document.body.appendChild(img);
                }, false);
                req.open("post", "./tmp_50.php");
                var sBoundary = "---------------------------" + Date.now().toString(16);
                req.setRequestHeader("Upgrade-Insecure-Requests", "1");
                req.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + sBoundary);
                var data = new Blob(["--" + sBoundary + "
    " + "Content-Disposition: form-data; name="payload"; filename="payload.data"" + "
    " + "Content-Type: application/octet-stream" + "
    " + "
    ", reader.result, "
    " + "--" + sBoundary + "--" + "
    "], {type : 'application/octet-stream'});
    
                req.send(data);
            });
        }, false);
    
    </script>
    </body>
    </html>

    PHP:

    <?php
    header("Content-Type: image/jpeg");
    //move_uploaded_file($_FILES['payload']['tmp_name'], './files/tmp_50.jpg');
    echo file_get_contents($_FILES['payload']['tmp_name']);
    ?>
  • 相关阅读:
    mysql 分页查询的优化
    将某盘下所有文件名存在一个文件下面
    java 时间处理经典案例
    完整的发邮件并且生成测试报告的例子
    python 定时任务的执行
    打飞机游戏第一天,诸神归位
    数据库中插入几百万条数据
    面向对象的总结
    Python关于文件操作的总结
    python自动化,自动登录并且添加一个门店
  • 原文地址:https://www.cnblogs.com/yiyide266/p/8566528.html
Copyright © 2020-2023  润新知