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

表单是网站常用的,不可缺少的。而通过表单建立图片上传也是刚需,基本每个网站都需要图片上传功能,现在比较流行的是直接无刷新上传图片,无刷新上传图片在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>',
    );
}
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 模块,就能实现无刷新上传,并且还支持拖拽,挺好的。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索replace
, array
配合
drupal form api、drupal webform、drupal form、drupal get form、drupal webform教程,以便于您获取更多的相关知识。

时间: 2024-08-14 08:27:55

Drupal7通过form API 建立无刷新的图片上传功能的四个方法的相关文章

javascript入门&amp;#183;图片对象(无刷新变换图片)\滚动图像_图象特效

复制代码 代码如下: <%@LANGUAGE="JAVASCRIPT" CODEPAGE="936"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://

HTML5 History API实现无刷新跳转

 在HTML5中, 1. 新增了通过JS在浏览器历史记录中添加项目的功能.       2. 在不刷新页面的前提下显示改变浏览器地址栏中的URL.       3. 添加了当用户单击浏览器的后退按钮时触发的事件.  通过以上三点,可以实现在不刷新页面的前提下动态改变浏览器地址栏中的URL,动态显示页面内容.  比如: 当页面A和页面B内容不一样的时候,在HTML5之前,如果从页面A切换到页面B时,需要在浏览器下从页面A切换到页面B,或者说,如果需要有后退按钮功 能的话,可以在URL地址加#XXX

ASP.NET实例:无刷新的文件上传

程序说明 [upload] 程序中最重要的方法就是upload了,调用它就可以进行无刷新上传.upload的过程是这样的,首先用stop方法停止上一次上传,并判断是否选择文件.然后分别调用_setIframe,_setForm和_setInput,生成需要的iframe,form和input.如果设置了timeout属性的话,会自动设置计时器: 以下为引用的内容:if ( this.timeout > 0 ) {    this._timer = setTimeout( $$F.bind(thi

Ajax无刷新实现图片切换特效

ajax|刷新|无刷新 一.AjaxMethodusing System;using System.Data;using System.Data.SqlClient; namespace AjaxImage{    /**//// <summary>    /// AjaxMethod 的摘要说明.    /// </summary>    public class AjaxMethod    {        public AjaxMethod()        {       

AJAX PHP无刷新上传图片实例代码

之前一直在研究ajax+php的表单无刷新验证,主要是用在注册提交表单上面的,ajax技术的使用使访客对于网页的友好度大大增加,作为提升页面友好的最主要技术,ajax是必不可少的. 当然,ajax不仅仅只有表单的无刷新验证,还可以更好地应用到页面的其它地方,凡是无刷新的地方基本上都有ajax技术的身影,今天讨论的是ajax+php无刷新上传图片.   无刷新上传图片的技术常常应用在上传附件或图片上传,比如常见的QQ邮箱上传附件,163邮箱上传附件,QQ空间上传图片等,这类都是应用了ajax无刷新

jQuery实现form表单基于ajax无刷新提交方法详解_jquery

本文实例讲述了jQuery实现form表单基于ajax无刷新提交方法.分享给大家供大家参考,具体如下: 首先,新建Login.html页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.o

实用ExtJS教程100例-009:ExtJS Form无刷新文件上传

文件上传在Web程序开发中必不可少,ExtJS Form中有一个filefield字段,用来选择文件并上传.今天我们来演示一下如何通过filefield实现ExtJS Form无刷新的文件上传. 首先,我们创建一个Form,它包含一个filefield字段 然后,我们通过Form的submit方法进行提交,此时,ExtJS会自动判断,如果Form中包含filefield字段,Form的method会设置为post 最后,我们通过服务器接收form提交的数据,并返回一段json字符串 ExtJS

一个无刷新二级联动下拉列表,同样适用与firefox,这算ajax么?

ajax|刷新|无刷新|下拉|下拉列表 可能"极好的"又会带来很多的非议,但是我认为这确实很好,我看了大约20个无刷新的连动下拉列表,他们在firefox下面就一团糟.为了这个我差不多搞了两天,就是如果提交窗体后如何保持第二个列表框的值,因为通过js 给下拉框添加条目那么他的状态是不会被保存的,不知道这算ajax么? 测试平台:ie6,firefox 功能:二级无刷新连动 特点:跨浏览器;提交窗体取第二下拉框的值;数据来源于数据库;以xmlhttp来发送请求,实现无刷新 请求:如果您能

适用与firefox ASP.NET无刷新二级联动下拉列表_实用技巧

可能"极好的"又会带来很多的非议,但是我认为这确实很好,我看了大约20个无刷新的连动下拉列表,他们在firefox下面就一团糟.为了这个我差不多搞了两天,就是如果提交窗体后如何保持第二个列表框的值,因为通过js 给下拉框添加条目那么他的状态是不会被保存的测试平台:ie6,firefox  功能:二级无刷新连动  特点:跨浏览器;提交窗体取第二下拉框的值;数据来源于数据库;以xmlhttp来发送请求,实现无刷新  请求:如果您能够找到更好的方法请告诉我,非常感谢,您的批评和建议对我是莫大