开发者

Using Ajax with cakephp to upload files

开发者 https://www.devze.com 2023-01-05 14:57 出处:网络
I have built an cakephp site that lets users upload files, all works fine but I would like to show a image saying \"file uploading\" or something while it is uploading. Now I know that I should use th

I have built an cakephp site that lets users upload files, all works fine but I would like to show a image saying "file uploading" or something while it is uploading. Now I know that I should use the $ajax->form helpers but I can not seem to get this to work. Can some one point me in the right direction please

<?php echo $ajax->form(array('type' => 'post',
    'options' => array(
        'model'=>'Upload',
        'update'=>'post2',
        'action' => 'add'开发者_StackOverflow中文版,
        'confirm' => 'Are you sure?',
        'indicator' => 'loading',
        'before' => '$("#post2").html("Wait a moment")'

    )
)); 
 ?>

    <fieldset>
        <legend><?php __('Add Upload');?></legend>

    <?php echo $form->input('title'); ?>

    <div id="formUpdate">

</div>

    <?php echo $form->input('Uploaded.uploaded_file', array('type' => 'file', 'label' => 'Upload file')); ?>


    </fieldset>
<?php echo $form->end('Submit');?>
</div>

    <div id="post2"></div>


Ok, if you don't want to use Ajax upload solution, then it could be achieved with simple javascript. I would explain how to do with jQuery, but you can achieve with any library or pure javascript too.

So, create a hidden div containing your please wait message i.e.:

<div id="wait">File is uploading please wait</div>

Then on the submit button of your form attach following function:

function pleaseWait(){
  $('#your_form_id').hide();
  $('#wait').show();
}

This basically would hide the form and will display the wait container. Once the file has been uploaded, the page will be redirected so, you don't need to worry how to show the form again.

I've did it on my project and it was working pretty well.

0

精彩评论

暂无评论...
验证码 换一张
取 消