佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > ajaxFileupload實(shí)現(xiàn)多文件上傳功能

ajaxFileupload實(shí)現(xiàn)多文件上傳功能

熱門標(biāo)簽:Linux服務(wù)器 團(tuán)購網(wǎng)站 服務(wù)器配置 阿里云 銀行業(yè)務(wù) 科大訊飛語音識(shí)別系統(tǒng) Mysql連接數(shù)設(shè)置 電子圍欄

打開google 搜索"ajaxFileupload' ‘多文件上傳"可以搜到許許多多類似的,那我為什么還要寫一下呢?
一個(gè)是對(duì)之前大神的貢獻(xiàn)表示感謝;二個(gè)是自己知識(shí)的總結(jié);三個(gè)是自己在原有的基礎(chǔ)上改動(dòng)了下,在此記錄,可能幫助其他朋友。

用過這個(gè)插件的都知道這個(gè)插件的基本用法,我就不廢話,直接上代碼。

我需要實(shí)現(xiàn)多個(gè)文件上傳,之前的做法是定義多個(gè)不同id的input,然后把a(bǔ)jaxfileuplod方法放在for循環(huán)里,這個(gè)方法是在網(wǎng)上看到的,我覺得不怎么好,后面在網(wǎng)上找到的,就高級(jí)點(diǎn)了,直接改源碼(因?yàn)樽髡吆镁脹]有跟新了,也確實(shí)滿足不了要求了)。接下來看看我是怎么改的。

引用網(wǎng)上的做法:

1、看沒有修改前的代碼

var oldElement = jQuery('#' + fileElementId); 
var newElement = jQuery(oldElement).clone(); 
jQuery(oldElement).attr('id', fileId); 
jQuery(oldElement).before(newElement); 
jQuery(oldElement).appendTo(form); 

很容易看出,這個(gè)就是把id為什么的input加到from里去,那么要實(shí)現(xiàn)多個(gè)文件上傳,就改成下面的樣子:

if(typeof(fileElementId) == 'string'){ 
 fileElementId = [fileElementId]; 
} 
for(var i in fileElementId){ 
 var oldElement = jQuery('#' + fileElementId[i]); 
 var newElement = jQuery(oldElement).clone(); 
 jQuery(oldElement).attr('id', fileId); 
 jQuery(oldElement).before(newElement); 
 jQuery(oldElement).appendTo(form); 
} 

 這樣改之后,初始化的代碼就要這么寫:

$.ajaxFileUpload({ 
 url:'/ajax.php', 
 fileElementId:['id1','id2']//原先是fileElementId:'id' 只能上傳一個(gè) 
}); 

到這里,確實(shí)可以上傳多個(gè)文件,但是對(duì)于我來說新問題又來,多個(gè)id,我的界面的文件不是固定的,是動(dòng)態(tài)加載的,那么id要?jiǎng)討B(tài)生成,我覺得太麻煩,為什么不取name呢?然后把以上代碼改為如下:

if(typeof(fileElementId) == 'string'){ 
   fileElementId = [fileElementId]; 
  } 
  for(var i in fileElementId){ 
   //按name取值 
   var oldElement = jQuery("input[name="+fileElementId[i]+"]"); 
   oldElement.each(function() { 
    var newElement = jQuery($(this)).clone(); 
    jQuery(oldElement).attr('id', fileId); 
    jQuery(oldElement).before(newElement); 
    jQuery(oldElement).appendTo(form); 
   }); 
  } 

 這樣改了 那么就可以實(shí)現(xiàn)多組多個(gè)文件上傳,接下來看我是怎么應(yīng)用的。

html:

div> 
    img id="loading" src="scripts/ajaxFileUploader/loading.gif" style="display:none;"> 
    
     table cellpadding="0" cellspacing="0" class="tableForm" id="calculation_model"> 
      thead> 
      tr> 
       th>多組多個(gè)文件/th> 
      /tr> 
      /thead> 
      tbody> 
      tr> 
       td>第一組/td> 
       td>第二組/td> 
      /tr> 
      tr> 
       td>input type="file" name="gridDoc" class="input">/td> 
       td>input type="file" name="caseDoc" class="input">/td> 
      /tr> 
      /tbody> 
      tfoot> 
      tr> 
       td>button class="button" id="up1">Upload/button>/td> 
       td>button class="button" id="addInput" >添加一組/button>/td> 
      /tr> 
      /tfoot> 
     /table> 
   /div> 

js:

/** 
 * Created with IntelliJ IDEA. 
 * User: Administrator 
 * Date: 13-7-3 
 * Time: 上午9:20 
 * To change this template use File | Settings | File Templates. 
 */ 
$(document).ready(function () { 
 $("#up1").click(function(){ 
  var temp = ["gridDoc","caseDoc"]; 
  ajaxFileUpload(temp); 
 }); 
 
 $("#addInput").click(function(){ 
  addInputFile(); 
 }); 
 
}); 
 
//動(dòng)態(tài)添加一組文件 
function addInputFile(){ 
 $("#calculation_model").append(" tr>"+ 
  "td>input type='file' name='gridDoc' class='input'>/td> "+ 
  "td>input type='file' name='caseDoc' class='input'>/td> "+ 
  "/tr>"); 
} 
 
 
//直接使用下載下來的文件里的demo代碼 
function ajaxFileUpload(id) 
{ 
 //starting setting some animation when the ajax starts and completes 
 $("#loading").ajaxStart(function(){ 
   $(this).show(); 
  }).ajaxComplete(function(){ 
   $(this).hide(); 
  }); 
 
 /* 
  prepareing ajax file upload 
  url: the url of script file handling the uploaded files 
  fileElementId: the file type of input element id and it will be the index of $_FILES Array() 
  dataType: it support json, xml 
  secureuri:use secure protocol 
  success: call back function when the ajax complete 
  error: callback function when the ajax failed 
 
  */ 
 $.ajaxFileUpload({ 
   url:'upload.action', 
   //secureuri:false, 
   fileElementId:id, 
   dataType: 'json' 
  } 
 ) 
 
 return false; 
 
} 

我后臺(tái)是用的struts2,strtus2的上傳是比較簡單的,只要聲明約定的名字,即可得到文件對(duì)象,和名稱,代碼如下:

package com.ssy.action; 
 
import com.opensymphony.xwork2.ActionSupport; 
import org.apache.commons.io.FileUtils; 
import org.apache.struts2.util.ServletContextAware; 
 
import javax.servlet.ServletContext; 
import java.io.*; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.Random; 
 
/** 
 * Created with IntelliJ IDEA. 
 * User: Administrator 
 * Date: 13-7-2 
 * Time: 下午4:08 
 * To change this template use File | Settings | File Templates. 
 */ 
public class Fileupload extends ActionSupport implements ServletContextAware { 
 private File[] gridDoc,caseDoc; 
 private String[] gridDocFileName,caseDocFileName; 
 
 private ServletContext context; 
 
  
 
 public String execute(){ 
  for (int i = 0;igridDocFileName.length;i++) { 
   System.out.println(gridDocFileName[i]); 
  } 
  for (int i = 0;icaseDocFileName.length;i++) { 
   System.out.println(caseDocFileName[i]); 
  } 
 
 
  //System.out.println(doc1FileName); 
  //System.out.println(doc2FileName); 
  String targetDirectory = context.getRealPath("/uploadFile"); 
 
  /* 
   *這里我只取得 第一組的文件進(jìn)行上傳,第二組的類似 
  */ 
 try{ 
   for (int i = 0; i  gridDoc.length; i++) { 
    String targetFileName = generateFileName(gridDocFileName[i]); 
    File target = new File(targetDirectory, targetFileName); 
    FileUtils.copyFile(gridDoc[i], target); 
   } 
  }catch (Exception e){ 
   e.printStackTrace(); 
  }  
 
  return SUCCESS; 
 } 
 
 public File[] getGridDoc() { 
  return gridDoc; 
 } 
 
 public void setGridDoc(File[] gridDoc) { 
  this.gridDoc = gridDoc; 
 } 
 
 public File[] getCaseDoc() { 
  return caseDoc; 
 } 
 
 public void setCaseDoc(File[] caseDoc) { 
  this.caseDoc = caseDoc; 
 } 
 
 public String[] getGridDocFileName() { 
  return gridDocFileName; 
 } 
 
 public void setGridDocFileName(String[] gridDocFileName) { 
  this.gridDocFileName = gridDocFileName; 
 } 
 
 public String[] getCaseDocFileName() { 
  return caseDocFileName; 
 } 
 
 public void setCaseDocFileName(String[] caseDocFileName) { 
  this.caseDocFileName = caseDocFileName; 
 } 
 
 /** 
  * 用日期和隨機(jī)數(shù)格式化文件名避免沖突 
  * @param fileName 
  * @return 
  */ 
 private String generateFileName(String fileName) { 
  System.out.println(fileName); 
  SimpleDateFormat sf = new SimpleDateFormat("yyMMddHHmmss"); 
  String formatDate = sf.format(new Date()); 
  int random = new Random().nextInt(10000); 
  int position = fileName.lastIndexOf("."); 
  String extension = fileName.substring(position); 
  return formatDate + random + extension; 
 } 
 
} 

寫到這里,我就有疑問了,之前的大神改的多文件,為什么還是取id,而且后臺(tái)是怎么取的,我還是沒怎么弄明白,我改的這個(gè)代碼可行么?是不是存在bug呢?這個(gè)還有待考驗(yàn),如果看出問題,請(qǐng)指出,共同學(xué)習(xí) 

最后附上,我修改后的插件

ajaxfileupload插件

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 基于jquery ajax的多文件上傳進(jìn)度條過程解析
  • 利用SpringMVC和Ajax實(shí)現(xiàn)文件上傳功能
  • PHP實(shí)現(xiàn)帶進(jìn)度條的Ajax文件上傳功能示例
  • php+ajax 文件上傳代碼實(shí)例
  • AjaxUpLoad.js實(shí)現(xiàn)文件上傳
  • AjaxUpLoad.js實(shí)現(xiàn)文件上傳功能
  • php+ajax實(shí)現(xiàn)無刷新文件上傳功能(ajaxuploadfile)
  • AjaxFileUpload+Struts2實(shí)現(xiàn)多文件上傳功能
  • AjaxFileUpload結(jié)合Struts2實(shí)現(xiàn)多文件上傳(動(dòng)態(tài)添加文件上傳框)
  • Ajax實(shí)現(xiàn)文件上傳功能(Spring MVC)

標(biāo)簽:大理 衡水 衢州 蚌埠 江蘇 萍鄉(xiāng) 廣元 棗莊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ajaxFileupload實(shí)現(xiàn)多文件上傳功能》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    洪江市| 灵宝市| 湟源县| 和田市| 凤山市| 稻城县| 渑池县| 新余市| 临猗县| 三台县| 邢台市| 广安市| 斗六市| 衡南县| 沁源县| 垦利县| 华池县| 湖南省| 中宁县| 桦川县| 庆云县| 九龙县| 简阳市| 马尔康县| 大名县| 东城区| 富锦市| 正镶白旗| 北安市| 武威市| 榕江县| 洛阳市| 应城市| 北票市| 普格县| 晋州市| 文昌市| 抚远县| 双桥区| 娱乐| 白城市|