Jquery.ocupload --- 文件上传入门

一、文件上传简单示例

1.1 引入jquery.ocupload

 

<!-- 导入jquery核心类库 -->
		<script type="text/javascript" src="../../js/jquery-1.8.3.js"></script>
		<!-- 导入ocupload -->
		<script type="text/javascript" src="../../js/ocupload/jquery.ocupload-1.1.2.js" ></script>

 

 

1.2 前端代码

 

// 为导入按钮添加一键上传效果
				$("#button-import").upload({
					//默认name为file,enctype默认提交方式为multipart/form-data
					action : "../../area_batchImport.action",
					//选中文件时候触发的事件
					onSelect:function () {
						//选中文件后,关闭自动提交
						this.autoSubmit = false;
						//判定文件格式,以.xls或者.xlsx结尾
						var filename =  this.filename();

						//校验的2种写法
						var reqex = /^.*\.(xls|xlsx)$/
						var reqex1 = new RegExp(".xls|.xlsx");
                        //alert(reqex.test(filename));
                        //alert(reqex1.test(filename));

						if(reqex.test(filename)){
							this.submit();
						}else{
						    $.messager.alert("警告","只能上传.xls或.xlsx结尾的文件","warning")
						}
                    },
					//文件上传后触发的事件
                    onComplete:function () {
						alert("文件上传成功");
                    }
				});


1.3 后端代码

 

 

 

 

//接收上传的文件
    private File file;

    public void setFile(File file) {
        this.file = file;
    }

    //批量区域数据导入
    @Action(value = "area_batchImport")
    public String batchImport() throws IOException {
        List<Area> areas = new ArrayList<>();

        //编写解析代码逻辑
        //基于.xls格式解析文件
        //1、加载Excel文件对象
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));

        //2、读取一个sheet
        HSSFSheet sheet = hssfWorkbook.getSheetAt(0);

        //3、读取sheet中每一行
        for (Row row : sheet){
            //一行数据对应一个区域对象
            Area area = new Area();
            if (row.getRowNum() == 0){
                //跳过第一行表头
                continue;
            }

            //控制结束
            if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())){
                continue;
            }

            area.setId(row.getCell(0).getStringCellValue());
            area.setProvince(row.getCell(1).getStringCellValue());
            area.setCity(row.getCell(2).getStringCellValue());
            area.setDistrict(row.getCell(3).getStringCellValue());
            area.setPostcode(row.getCell(4).getStringCellValue());

            //基于pinyin4j生成城市编码和简码
            String province = area.getProvince();
            String city = area.getCity();
            String district = area.getDistrict();
            province = province.substring(0,province.length() - 1);
            city = city.substring(0,city.length() - 1);
            district = district.substring(0,district.length() - 1);

            //简码
            String[] headArray = PinYin4jUtils.getHeadByString(province + city + district);
            StringBuffer buffer = new StringBuffer();
            for (String headStr : headArray) {
                buffer.append(headStr);
            }
            String shortcode = buffer.toString();
            area.setShortcode(shortcode);

            //城市编码
            String citycode = PinYin4jUtils.hanziToPinyin(city,"");
            area.setCitycode(citycode);

            areas.add(area);
        }

        //调用业务层,保存数据
        areaService.saveBatch(areas);

        return NONE;
    }

 

 

 

 

 

二、Jquery.ocupload官方文档

 

http://code.google.com/p/ocupload/

This documentation covers the following. 

    * Basic Usage 
    * Options 
    * Functions 
    * Callbacks 

Basic Usage 

As a jQuery chained function 

//方式一:
$(element).upload( /** options */ ); 

As a stand-alone jQuery function 

//方式二:
$.ocupload($(element), /** options */ ); 

Options 

Both of the functions accept an optional options object, which can contain any or all of the following (default value): 
	//文件上传的名字,默认值是file
    * name: ("file") The name of the file input form element. 
	//文件提交的方式,默认值是multipart/form-data
    * enctype: ("multipart/form-data") The enctype attribute of the form, it is usually a good idea to leave this as default. 
    //文件上传的action
	* action: (null) The form action attribute, the page that will do the processing on the server side. 
    //文件输入框发生变化时,自动提交
	* autoSubmit: (true) If true the form will be submitted after the user selects a file (after the file browser closes). 
    //文件上传时的额外参数
	* params: ({}) Additional paramaters to be sent with the file, creates a hidden form element using the key as the name of the form and the value as the value. 
    //提交时触发的事件
	* onSubmit: (null) The callback function called before submitting the form. 
    //文件上传后触发的事件
	* onComplete: (null) The callback function called after the action page loads. 
    //选中文件时触发的事件
	* onSelect: (null) The callback function called after the user has selected a file. 

Example 

var myUpload = $(element).upload({ 
        name: 'file', 
        action: '', 
        enctype: 'multipart/form-data', 
        params: {}, 
        autoSubmit: true, 
        onSubmit: function() {}, 
        onComplete: function() {}, 
        onSelect: function() {} 
}); 

Functions 
filename 
Description 

string filename ( void ) 

This function returns the name of the currently selected file. 
Example 

var myUpload = $(element).upload(); 
alert(myUpload.filename()); 

name 
Description 

string name ( void ) 
void name ( string ) 

This function is used to get and set the name of the file input. 
Example 

//Setting name at creation 
var myUpload = $(element).upload({ 
        name: 'myFile' 
}); 

//Changes the file input name to "myNewFile" 
myUpload.name('myNewFile'); 

//Alerts "myNewFile" 
alert(myUpload.name()); 

action 
Description 

string action ( void ) 
void action ( string ) 

This function is used to get and set the action of the form. 
Example 

//Setting action at creation 
var myUpload = $(element).upload({ 
        action: 'upload.php' 
}); 

//Changes the form action to "path/to/dir/newUpload.php" 
myUpload.action('path/to/dir/newUpload.php'); 

//Alerts "path/to/dir/newUpload.php" 
alert(myUpload.action()); 

enctype 
Description 

string enctype ( void ) 
void enctype ( string ) 

This function is used to get and set the enctype of the form. 
Example 

//Setting enctype at creation 
var myUpload = $(element).upload({ 
        enctype: 'multipart/form-data' 
}); 

//Changes the form enctype to "application/x-www-form-urlencoded" 
myUpload.enctype('application/x-www-form-urlencoded'); 

//Alerts "text/plain" 
alert(myUpload.enctype()); 

params 
Description 

object params ( void ) 
void params ( object ) 

This function is used to alter additional parameters. 
Example 

//Setting paramters at creation 
var myUpload = $(element).upload({ 
        params: {name: 'My file', description: 'My file description'}  
}); 

/** 
* Settings paramaters during runtime 
*      name: "My file" is replaced with "My new file 
*      description: remains the same 
*      size: is added 
*/ 
myUpload.params({ 
        name: 'My new file', size: '1000kb' 
}); 

set 
Description 

void set ( object ) 

This function is used to alter options after creation of the object. 
Example 

//Setting options at creation 
var myUpload = $(element).upload( /** options */ ); 

//Setting options after creation 
myUpload.set({ 
        name: 'file', 
        action: '', 
        enctype: 'multipart/form-data', 
        params: {}, 
        autoSubmit: true, 
        onSubmit: function() {}, 
        onComplete: function() {}, 
        onSelect: function() {} 
}); 

submit 
Description 

void submit ( object ) 

This function is used to submit the form if autoSubmit is turned off. 
Example 

Javascript 

var myUpload = $(element).upload( /** options */ ); 

HTML 

<input type="button" value="submit" onclick="myUpload.submit()" /> 

Callbacks 
onSubmit 

void onSubmit ( void ) 
Description 

This is called before the form is submitted, this is where you should make last minute changes to the parameters etc... 
onComplete 

void onComplete ( response ) 
Description 

This is called after the action page has loaded, the first parameter contains the html response from the action so you can use it like an AJAX response. onComplete does not mean the file was uploaded successfully, you should use the action script to supply a suitable response. 
onSelect 

void onSelect ( void ) 
Description 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: jquery.jqprint-0.3.js 是一个基于 jQuery 的插件,用于在网页中实现打印功能。它可以让用户通过点击页面上的一个按钮或者执行一个 JavaScript 函数来实现将当前页面的内容打印出来的功能。 这个插件的使用非常简单,只需要在页面中引入 jquery.jqprint-0.3.js 文件,并在需要的地方调用插件提供的方法即可。插件提供的方法主要有 `jqprint()` 方法,用于执行页面的打印操作。例如,可以在页面上有一个按钮,点击按钮时执行 `jqprint()` 方法来触发打印。 插件的主要功能是将页面内容转化为打印格式,并调用浏览器的打印功能进行实际打印。在执行 `jqprint()` 方法时,插件会创建一个隐藏的 iframe 元素,并将页面内容复制到该 iframe 中。然后,插件会调用 iframe 的打印方法,实现将内容打印出来的效果。 除了基本的打印功能外,jquery.jqprint-0.3.js 还提供了一些可配置的选项,允许用户在打印之前进行一些自定义操作。比如可以设置打印页眉、页脚的内容,还可以设置打印页面的比例等。 总之,jquery.jqprint-0.3.js 是一个方便实用的 jQuery 插件,可以在网页中轻松实现打印功能,使用户能够将页面内容快速、方便地打印出来。它的简单易用和可配置性使得它适用于各种需要打印功能的网页应用。 ### 回答2: jquery.jqprint-0.3.js是一个基于jQuery的打印插件。它提供了一个简单的方法来打印一个页面或特定的DOM元素。 使用该插件非常简单,只需加载jQueryjquery.jqprint-0.3.js文件,并调用相应的方法即可。通过调用$.jqprint()方法,可以打印整个页面。 如果要打印指定的DOM元素,可以将该元素的选择器传递给$.jqprint()方法。例如,要打印id为"myElement"的元素,可以使用$("#myElement").jqprint()。 除了基本的打印功能外,jquery.jqprint-0.3.js还支持一些其他的选项和回调函数。例如,可以通过设置{noPrintSelector: ".no-print"}来指定不应打印的元素的选择器;可以使用beforePrint和afterPrint回调函数在打印之前和之后执行一些自定义的操作。 总结来说,jquery.jqprint-0.3.js是一个方便易用的打印插件,可用于在网页中实现打印功能。无论是打印整个页面还是指定的DOM元素,该插件都提供了简单的方法,并且可以通过一些选项和回调函数进行更多的定制。 ### 回答3: jquery.jqprint-0.3.js是一个jQuery插件,用于在浏览器中打印HTML内容。它提供了一种简单的方式来将特定区域的内容打印出来,而无需使用浏览器的默认打印功能。 使用jquery.jqprint-0.3.js,我们可以通过选择器来指定要打印的HTML区域。可以是整个页面、特定的div或任何其他HTML元素。该插件会自动将指定的内容复制到一个隐藏的iframe中,并使用浏览器的打印功能将其打印出来。 该插件还提供了一些自定义选项,使我们能够控制打印的一些方面。例如,我们可以设置打印时的页眉和页脚,设置打印页面的标题,调整打印页面的大小等等。这些选项可以通过传递一个包含设置参数的对象来实现。 使用jquery.jqprint-0.3.js非常简单。只需在HTML页面中引入jQuery库和插件文件,然后在需要打印的按钮或链接的click事件中调用`$().jqprint()`方法即可。例如,可以使用以下代码来实现点击一个按钮后打印指定区域的内容: ``` $("#print-button").click(function() { $("#print-content").jqprint(); }); ``` 总之,jquery.jqprint-0.3.js是一个方便的jQuery插件,提供了一种简单的方法来在浏览器中打印HTML内容。通过选择器和自定义选项,我们可以控制打印的内容和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值