我正在使用拖放功能上传图片以替换用户头像。一切正常,但是在上传完成后,我很难确定将代码放置在哪里进行重定向。
这是我的代码:
<script>
Dropzone.options.myDropzone = {
addRemoveLinks: true,
init: function() {
thisDropzone = this;
//listen to removedfile event
thisDropzone.on('removedfile', function(file){
$.get('avatar_handler.php?userid=id<?php echo $id2use; ? >&action=remove&page=avatar&name='+file.name);
});
}
}
</script>
和我的处理程序脚本:
switch( $action ) {
case 'upload':
if ( !empty($_FILES) ) {
storeFile($_FILES);
}
break;
case 'remove':
$filename = $_GET['name'];
removeFile( $filename );
break;
case 'show':
showFiles();
break;
}
function showFiles()
{
$result = array();
$files = scandir(DES);
if ( false!==$files ) {
foreach ( $files as $file ) {
//ignore current and parent folder indicator
if ( '.'!=$file && '..'!=$file) {
$obj['name'] = $file;
$obj['size'] = filesize(DES.$file);
$result[] = $obj;
}
}
}
header('Content-type: text/json');
header('Content-type: application/json');
echo json_encode($result);
}
function storeFile( $file )
{
$tempFile = $file['file']['tmp_name'];
//move file to server
$targetPath = dirname( __FILE__ ) . DS. DES ;
$targetFile = $targetPath. time().$file['file']['name'];
** THIS IS WHERE I WANT TO DO A REDIRECT - after files is saved **
}
function removeFile( $fileName ) {
$targetPath = dirname( __FILE__ ) . DS. DES ;
$targetFile = $targetPath. $fileName;
//remove only when file exists
if ( file_exists($targetFile) ) {
unlink($targetFile);
}
}
每当我尝试添加到处理程序中时。脚本错误
//show stored files
$.get('avatar_handler.php?userid=id<?php echo $id2use; ?>&action=show&page=avatar', function(data) {
$.each(data, function(key,value){
var mockFile = { name: value.name, size: value.size };
thisDropzone.options.addedfile.call(thisDropzone, mockFile);
thisDropzone.options.thumbnail.call(thisDropzone, mockFile,
"usermedia/id<?php echo getuserid(); ?>/avatar/"+value.name);
});
},success: function(msg) {
window.location = 'http://mymusicwall.co.uk';
});
以上是我尝试过的。史提芬
最佳答案
您可以将重定向放入success
调用的$.get
函数中:
$.get(
url: 'avatar_handler.php',
data: {
userid: 'id<?php echo $id2use;?>',
action: 'remove',
page: 'avatar',
name: file.name
},
success: function(msg) {
window.location = 'http://yourredirectlocation.com';
}
);
文件:http://api.jquery.com/jquery.get/