2014-01-24 33 views
1

我使用拖動&拖放上傳圖像來替換用戶頭像。它工作的很好,但是在上傳完成後,我很難確定將重定向代碼放在哪裏。如何在拖放上傳完成後重定向到URL

這裏是我的代碼:

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

以上是我試過的。斯蒂芬

回答

0

你會放一個重定向到您的$.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/

+0

嗨,感謝您的快速回復。我嘗試了以下但沒有成功。 –

+0

問題必須在其他地方。我建議你編輯你的問題,並更具體地說明問題出在哪裏。 –

+0

嗨,我編輯它,只是等待批准 - 謝謝 –

0

使用:

document.location = '<redirect_location_url>'; 
在拖動功能

成功

相關問題