gpt4 book ai didi

javascript - 拖放上传完成后如何重定向到网址

转载 作者:行者123 更新时间:2023-11-28 08:35:08 25 4
gpt4 key购买 nike

我正在使用拖放功能上传图像以替换用户头像。它工作正常,但我很难确定上传完成后将重定向代码放在哪里。

这是我的代码:

<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';
});

以上是我尝试过的。斯蒂芬

最佳答案

您可以将重定向放入 $.get 调用的 success 函数中:

$.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/

关于javascript - 拖放上传完成后如何重定向到网址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21323791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com