Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Qiniu_JavaScript_PHP_Upload | 原生 JS 与 PHP 实现无刷新上传文件至七牛云
<script>
var upload = function() {
var pre_name = new Date().getTime();
var suffix = get_suffix($("#file")[0].files[0].name);
var name = pre_name + suffix;
var config = {
useCdnDomain: true
};
var token = '<?php use Qiniu\Auth;
$bucket = 'xx';
$accessKey = 'xxx';
$secretKey = 'xxx';
$auth = new Auth($accessKey, $secretKey);
$upToken = $auth->uploadToken($bucket);
header('Access-Control-Allow-Origin:*');
echo $upToken; ?>';
var file = $("#file")[0].files[0];
var observable = qiniu.upload(file, name, token, config)
var observer = {
next(res) {
console.log(res.total.percent);
},
error(err) {
console.log(err.message);
},
complete(res) {
console.log('https://static.ouorz.com/' + name);
}
}
var subscription = observable.subscribe(observer);
}
var get_suffix = function(name) {
var index = name.lastIndexOf('.');
return name.substring(index);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment