本文我們講講在Drupal 7通過form API 實現無刷新圖片上傳的幾個方法,比較實用,喜歡drupal建站的朋友可以參考一下.
表單是網站常用的,不可缺少的,而通過表單建立圖片上傳也是剛需,基本每個網站都需要圖片上傳功能,現在比較流行的是直接無刷新上傳圖片,無刷新上傳圖片在drupal7 如何做呢?下面代碼就是實現此功能。
方法1:用file原件配合ajax參數實現:
- function testmodule_forma($form, &$form_state){
- $form['im-container'] = array(
- '#prefix'=>'<div id="im-area">',
- '#suffix'=>'</div>',
- );
- $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;
- }
- function upload_image($form, $form_state){
- $file = file_save_upload('image_file', array('file_validate_extensions' => array('png gif jpg jpeg')), "public://",$replace = FILE_EXISTS_REPLACE);
- if ($file)
- {
- $file->status=FILE_STATUS_PERMANENT;
- file_save($file);
- $form['im-container']=array(
- '#title'=>t('Preview:'),
- '#prefix'=>'<div id="im-area">',
- '#markup'=>'<img src="sites/default/files/'.$file->filename.'" height="250" width="250" />',
- '#suffix'=>'</div>',
- ); //Vevb.com
- }
- else {
- drupal_set_message('No file uploaded.');
- }
- return $form['im-container'];
- }
方法2:直接使用 manage_file 原件實現:
上面的方式是需要配一個上傳按鈕,然而在drupal 7 有一個更好的表單原件 manage_file,可以通過manage_file實現無刷新上傳.
- $form['image_example_image_fid'] = array(
- '#title' => t('<a href="/project/image" class="alinks-link" title="模塊介紹:讓有特定權限的用戶可以上傳圖片到網站里,并且會自動產生縮圖。圖片可以使用在文章里(例如透過tinymce編輯工具進行選取),或是作成簡單的網絡相簿。">Image</a>'),
- '#type' => 'managed_file',
- '#description' => t('The uploaded image will be displayed on this page using the image style choosen below.'),
- '#default_value' => variable_get('image_example_image_fid', ''),
- '#upload_location' => 'public://image_example_images/',
- );
方法3:使用manage_file 原件 配合js 實現不需要點擊上傳按鈕直接上傳:
上面兩種方式都可以實現無刷新上傳,但界面并不友好,兩種方式都是需要點擊上傳按鈕才觸發上傳,我們更多時候是不想有上傳按鈕,下面這個方式就可以做到:
- File: auto_upload.info
- name = Auto Upload
- description = Removes the need for users to press the 'Upload' button for AJAX file uploads.
- core = 7.x
- dependencies[] = file
- File: auto_upload.js:
- (function ($) {
- Drupal.behaviors.autoUpload = {
- attach: function (<a href="/project/context" class="alinks-link" title="模塊介紹:就是“根據某些條件”顯示“某些區塊”">context</a>, settings) {
- $('form', context).delegate('input.form-file', 'change', function() {
- $(this).next('input[type="submit"]').mousedown();
- });
- }
- };
- })(jQuery);
- File: auto_upload.module
- function auto_upload_init() {
- drupal_add_js(drupal_get_path('module', 'auto_upload') . '/auto_upload.js');
- }
我們還可以再優化下,讓上傳圖片時候,顯示縮略圖:
- <?php
- /**
- * Implements hook_field_widget_form().
- */
- function multifield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
- //Get the default format for user
- $default_format = array_shift(filter_formats($GLOBALS['user']))->format;
- $field_name = $instance['field_name'];
- $item =& $items[$delta];
- switch($instance['widget']['type']) {
- case 'multifield_base_widget':
- $element['img1_upload'] = array(
- '#title' => t('Image'),
- '#type' => 'managed_file',
- '#upload_location' => 'public://multifield_images/',
- '#default_value' => isset($item['img1_upload']) ? $item['img1_upload'] : 0,
- // assign #theme directly to the managed_file in other case it won't be
- // rebuilt after file upload
- '#theme' => 'image_multifield_multitype',
- );
- }
- return $element;
- }
- /**
- * Implements hook_theme().
- */
- function multifield_theme() {
- return array(
- 'image_multifield_multitype' => array(
- 'render element' => 'element',
- ),
- );
- }
- /**
- * Returns HTML for a managed file element with thumbnail.
- */
- function theme_image_multifield_multitype($variables) {
- $element = $variables['element'];
- $output = '';
- if($element['fid']['#value'] != 0) {
- // if image is uploaded show its thumbnail to the output HTML
- $output .= '<div class="multifield-thumbnail">';
- //$output .= theme('image_style', array('style_name' => 'thumbnail', 'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE));
- $output .= '<img src="' . image_style_url('medium', file_load($element['fid']['#value'])->uri). '" class="thumbnail"/>';
- $output .= drupal_render_children($element); // renders rest of the element as usual
- $output .= '</div>';
- }
- return $output; // of course, has to be returned back
- }
- ?>
方法4:用第三方模塊
還有一種方式比較簡單直接,就是直接用第三方模塊,例如Drag & Drop Upload 模塊,就能實現無刷新上傳,并且還支持拖拽,挺好的.
新聞熱點
疑難解答