jQuery插件之Ajax Upload篇

本文介绍了如何使用jQuery插件实现Ajax Upload功能,包括创建uploader、实例化Ajax Uploader、配置上传限制以及处理服务器端脚本。示例中展示了PHP和ASPX的服务器端处理代码,并提供了解析上传文件和限制文件格式的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用方法:

建立uploader

使用如下语句建立上传按钮.

<div id="upload_button">Upload</div>

建立Ajax Uploader实例

使用如下代码

// You must create it only after the DOM is ready for manipulations
// Use $(document).ready for jquery
// document.observe("dom:loaded" for prototype
new AjaxUpload('upload_button_id', {action: 'upload.php'});
 
// 注: 如果我们使用的jquery框架, 我们可以这样加载Uploader
$(function(){
    new AjaxUpload('upload_button_id', {action: 'upload.php'});
})

 

配置Ajax Uploader

new AjaxUpload('#upload_button_id', {
  // 服务器端处理上传文件的脚本路径
  action: 'upload.php',
  // File upload name, 如在php中$_FILES['userfile']
  name: 'userfile',
  // 其他要同时post的数据
  data: {
    example_key1 : 'example_value',
    example_key2 : 'example_value2'
  },
  // Submit file after selection
  autoSubmit: true,
  // 文件被选择后触发
  // Useful when autoSubmit is disabled
  // You can return false to cancel upload
  // @param file basename of uploaded file
  // @param extension of that file
  onChange: function(file, extension){},
  // 文件被上传完成前触发
  // You can return false to cancel upload
  // @param file basename of uploaded file
  // @param extension of that file
  onSubmit: function(file, extension) {},
  // 文件上传完成后触发
  // @param file basename of uploaded file
  // @param response server response
  onComplete: function(file, response) {}
});

处理上传文件,在服务器端脚本中,我们可以通过下面的参数来获取上传文件

  • PHP: $_FILES['userfile']
  • Rails: params[:userfile]

userfile相当于html表单<input type=”file” name=”userfile” />中的name的值
其他参数的获取方法

  • PHP: $_POST['yourkey']
  • Rails: params[:yourkey]

服务器端脚本示例

php脚本

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "success";
} else {
  echo "error";
}

ASPX示例脚本

using System;
using System.Web;
using System.IO;
 
public class FileHandler : IHttpHandler
{
 
    public void ProcessRequest(HttpContext context)
    {
        string strFileName = Path.GetFileName(context.Request.Files[0].FileName);
        string strExtension = Path.GetExtension(context.Request.Files[0].FileName).ToLower();
        string strSaveLocation = context.Server.MapPath("Upload") + "" + strFileName;
        context.Request.Files[0].SaveAs(strSaveLocation);
 
        context.Response.ContentType = "text/plain";
        context.Response.Write("success");
    }
 
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

配置只允许上传指定格式的文件

我们可以在onSubmit回调函数中处理指定格式,如果不是正确的格式则return false来取消上传.

new AjaxUpload('#button2', {
        action: 'upload.php',
	onSubmit : function(file , ext){
		if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
			// extension is not allowed
			alert('Error: invalid file extension');
			// cancel upload
			return false;
		}
	}
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值