使用 formdata 结合 snoopy 做请求转发,实现非 form 形式,非 iframe 形式的异步文件上传功能。
JS 代码:
document.getElementById('#file').change = function (e) {
var data = new FormData();
//设置参数
data.append('nick', '邦彦');
data.append('session_id', '6fc373162b5e8a78a255777e0108a0b3');
data.append('file', e.currentTarget.files[0]);
}
PHP 代码:
<?php
require('Snoopy.class.php');
$action = 'http://localhost/upload.php';
$snoopy = new Snoopy();
$snoopy->_submit_type = 'multipart/form-data';
$snoopy->submit($action, $_REQUEST, $_FILES);
echo $snoopy->results;
// Downloads By http://www.veryhuo.com