Cms技巧
分享创造价值 合作实现共赢

Cms技巧

当前位置: 首页 > 新闻动态 > Cms技巧

Drupal7通过form API 建立无刷新的图片上传功能的四

发布时间:2019-12-16 11:15:29作者:admin点击:

表单是网站常用的,不可缺少的。而通过表单建立图片上传也是刚需,基本每个网站都需要图片上传功能,现在比较流行的是直接无刷新上传图片,无刷新上传图片在drupal7 如何做呢?下面代码就是实现此功能。

方法1:用file原件配合ajax参数实现:

functiontestmodule_forma($form,&$form_state){
$form['im-container']=array(
'#prefix'=>'',
'#suffix'=>'
', ); $form['image_file']=array( '#type'=>'file', ); $form['upload']=array( '#type'=>'submit', '#value'=>'upload', '#submit'=>array('upload_image'), '#ajax'=>array( 'callback'=>'upload_image', 'wrapper'=>'im-area', 'method'=>'replace', 'effect'=>'fade', ), ); return$form; } functionupload_image($form,$form_state){ $file=file_save_upload('image_file',array('file_validate_extensions'=>array('pnggifjpgjpeg')),"public://",$replace=FILE_EXISTS_REPLACE); if($file) { $file->status=FILE_STATUS_PERMANENT; file_save($file); $form['im-container']=array( '#title'=>t('Preview:'), '#prefix'=>'', '#markup'=>'filename.'"height="250"width="250"/>', '#suffix'=>'
', ); } else{ drupal_set_message('Nofileuploaded.'); } return$form['im-container']; }



方法2:直接使用 manage_file 原件实现:

上面的方式是需要配一个上传按钮,然而在drupal 7 有一个更好的表单原件 manage_file,可以通过manage_file实现无刷新上传。

$form['image_example_image_fid']=array(
'#title'=>t('Image'),
'#type'=>'managed_file',
'#description'=>t('Theuploadedimagewillbedisplayedonthispageusingtheimagestylechoosenbelow.'),
'#default_value'=>variable_get('image_example_image_fid',''),
'#upload_location'=>'public://image_example_images/',
); 
          

TOP

QQ客服

18910140161