uploadify & PHP - upload cancel & page refresh issue

為{幸葍}努か 提交于 2019-12-25 02:19:42

问题


I have integrated uploadify module in my existing programming and things work fine right now.

Now, I want to do 2 things.

1 - When I click cancel button during file uploading, file upload process is cancelled immediately and file does not upload on server but file name is stored in database. So how to prevent script so it doesn't store data in database when I cancel the uploading? Please help on this.

2 - Is is possible to refresh entire web page after file uploading process is completed? Please help on this too.

Many Thanks, KRA


回答1:


You can use OnUploadComplete event of uploadify for this. You can save the filename of the uploaded file on completion of the upload. so if you cancle the upload in between then it will not get stored in the database.

$(function() {
    $("#file_upload").uploadify({
        'swf'              : '/uploadify/uploadify.swf',
        'uploader'         : '/uploadify/uploadify.php',
        'onUploadComplete' : function(file) {
            alert('The file ' + file.name + ' finished processing.');
        }
    });
});

Also for second thing you can use location.reload(true) in above function to refresh the page on completion of the upload




回答2:


Try this

$targetFolder = $UPLOAD_PATH; // Relative to the root
$verifyToken = md5('unique_salt' . $_POST['timestamp']);
if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $targetFolder;
$targetFileName = $int_pkid.".".$targetFileExt;;
$targetFile = rtrim($targetPath,'/') . '/' . $targetFileName;

// Validate the file type
$fileTypes = array('pdf','doc','docx'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);

if (in_array($fileParts['extension'],$fileTypes)) {
    move_uploaded_file($tempFile,$targetFile);
    echo '1';

            //Will Enter this Block only when the upload is success
            //This should fix one of you
            $str_query_insert="INSERT INTO  tr_file_data (pkid,title,filename)";
            $str_query_insert.=" VALUES(".$int_pkid.",'".${str_title}."','".${targetFileName}."')";
            ExecuteQuery($str_query_insert);

            //To Reload, there are no straight legal ways, but can do with a twist
            //Method 1:
            Header('Location: '.$_SERVER['PHP_SELF']);
            Exit(); //optional
            //Method 2:
            echo '<script>parent.window.location.reload(true);</script>';




} else {
    echo 'Invalid file type.';
}
}


// To apply directly to uploadify
$(function() {
    $("#file_upload").uploadify({
        'swf'              : '/uploadify/uploadify.swf',
        'uploader'         : '/uploadify/uploadify.php',
        'onUploadComplete' : function(file) {
            alert('The file ' + file.name + ' finished proce`enter code here`ssing.');

                //Will Enter this Block only when the upload is success
                //This should fix one of you
                $str_query_insert="INSERT INTO  tr_file_data (pkid,title,filename)";
                $str_query_insert.=" VALUES(".$int_pkid.",'".${str_title}."','".${targetFileName}."')";
                ExecuteQuery($str_query_insert);

                //To Reload, there are no straight legal ways, but can do with a twist
                //Method 1:
                Header('Location: '.$_SERVER['PHP_SELF']);
                Exit(); //optional
                //Method 2:
                echo '<script>parent.window.location.reload(true);</script>';

        }
    });
});


来源:https://stackoverflow.com/questions/13538962/uploadify-php-upload-cancel-page-refresh-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!