jQuery插件ajaxfileupload实现无刷新文件上传详解
本文详细介绍如何使用jQuery插件ajaxfileupload实现无刷新文件上传。涵盖插件引入、HTML结构配置、JavaScript参数设置及常见错误排查,并提供完整的客户端与服务端代码示例,帮助开发者快速掌握Ajax文件上传技术。
jQuery ajaxfileupload插件简介与引入
jQuery插件AjaxFileUpload能够实现Ajax环境下的文件上传功能。该插件操作简便,下文将首先介绍其正确使用方法,随后梳理常见的报错信息及对应的解决方案。
使用说明
使用前需确保已引入jQuery库文件以及AjaxFileUpload库文件。
HTML结构与JavaScript配置详解
使用实例
一,包含文件部分
代码如下:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="ajaxfileupload.js"></script>
二,HTML部分
代码如下:
<img id="loading " src="loading.gif" style="display:none;">
<input id="fileToUpload " type="file" size="20" name="fileToUpload " class="input">
<button class="button" id="buttonUpload" onclick="return ajaxFileUpload ();">上传</button>
仅需三个核心元素:用于动态显示加载状态的小图标、文件选择域以及触发上传的按钮。需要注意的是,使用AjaxFileUpload插件进行文件上传时,无需手动创建form表单,具体结构如下:
<form name="form" action="" method="POST" enctype="multipart/form-data"> ……相关html代码…… </form> 因为AjaxFileUpload插件会自动生成一个form提交表单。
对于file文件域的ID和name属性,ajaxFileUpload插件的fileElementId参数用于获取文件域的ID;若需处理上传的文件信息,则必须明确文件域的name属性。这两者的对应关系至关重要。
三,javascript部分
<script type="text/javascript">
function ajaxFileUpload (){
loading();//动态加载小图标
$.ajaxFileUpload ({
url :'upload.php',
secureuri :false,
fileElementId :'fileToUpload',
dataType : 'json',
success : function (data, status){
if(typeof(data.error) != 'undefined'){
if(data.error != ''){
alert(data.error);
}else{
alert(data.msg);
}
}
},
error: function (data, status, e){
alert(e);
}
})
return false;
}
function loading (){
$("#loading ").ajaxStart(function(){
$(this).show();
}).ajaxComplete(function(){
$(this).hide();
});
}
</script>
主要参数说明: 1,url表示处理文件上传操作的文件路径,可以测试URL是否能在浏览器中直接访问,如上:upload.php 2,fileElementId表示文件域ID,如上:fileToUpload 3,secureuri是否启用安全提交,默认为false 4,dataType数据数据,一般选json,javascript的原生态 5,success提交成功后处理函数 6,error提交失败处理函数
上述示例包含两个关键方法:一个是用于动态加载小图标的提示函数loading(),另一个是执行文件上传的$.ajaxFileUpload()函数。其用法与jQuery.ajax()类似,操作十分简单。此处省略了PHP处理上传文件的具体逻辑,仅展示如何利用jQuery插件AjaxFileUpload实现Ajax文件上传,过程十分便捷。
常见错误排查与解决方案
同时我们需要了解相关的错误提示
1,SyntaxError: missing ; before statement错误 如果出现这个错误就需要检查url路径是否可以访问
2,SyntaxError: syntax error错误 如果出现这个错误就需要检查处理提交操作的PHP文件是否存在语法错误
3,SyntaxError: invalid property id错误 如果出现这个错误就需要检查属性ID是否存在
4,SyntaxError: missing } in XML expression错误 如果出现这个错误就需要检查文件域名称是否一致或不存在
5,其它自定义错误 大家可使用变量$error直接打印的方法检查各参数是否正确,比起上面这些无效的错误提示还是方便很多。
使用jQuery插件AjaxFileUpload实现无刷新上传文件非常实用,由于其简单易用,因些这个插件相比其它文件上传插件使用人数最多,非常值得推荐。 处理页面:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class web_ajax_FileUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection files = HttpContext.Current.Request.Files;
//if (files[0].ContentLength > 5)
//{
// Response.Write("{");
// Response.Write("msg:'" + files[0].FileName + "',");
// Response.Write("error:'文件上传失败'");
// Response.Write("}");
//}
//else
//{
// Response.Write("{");
// Response.Write("msg:'没有文件被上传',");
// Response.Write("error:'文件上传失败'");
// Response.Write("}");
//}
files[0].SaveAs("d:/adw.jpg");
Response.Write("{");
Response.Write("msg:'a',");
Response.Write("error:''");
Response.Write("}");
//Response.Write("{");
//Response.Write("msg:'ggg\n',");
//Response.Write("error:'aa\n'");
//Response.Write("}");
Response.End();
}
}
完整前后端代码示例
其它网友的补充:
页面代码:
代码如下:
<html>
<!-- 引入相关的js文件,相对路径 -->
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/ajaxfileupload.js"></script>
<!-- 执行上传文件操作的函数 -->
<script type="text/javascript">
function ajaxFileUpload(){
$.ajaxFileUpload(
{
url:'update.do?method=uploader', //需要链接到服务器地址
secureuri:false,
fileElementId:'houseMaps', //文件选择框的id属性
dataType: 'xml', //服务器返回的格式,可以是json
success: function (data, status) //相当于java中try语句块的用法
{
$('#result').html('添加成功');
},
error: function (data, status, e) //相当于java中catch语句块的用法
{
$('#result').html('添加失败');
}
}
);
}
</script>
</head>
<body>
<form method="post" action="update.do?method=uploader" enctype="multipart/form-data">
<input type="file" id="houseMaps" name="houseMaps"/>
<input type="button" value="提交" onclick="ajaxFileUpload()"/>
</form>
<div id="result"></div>
</body>
</html>
服务器代码:
代码如下:
public class UpdateAction extends DispatchAction {
public ActionForward uploader(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UpFormForm upFormForm = (UpFormForm) form;
FormFile ff = upFormForm.getHouseMaps();
try {
InputStream is = ff.getInputStream();
File file = new File("D:/" + ff.getFileName()); //指定文件存储的路径和文件名
OutputStream os = new FileOutputStream(file);
byte[] b = new byte[1024];
int len = 0;
while((len = is.read(b)) != -1){
os.write(b, 0, len);
}
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}JS中使用位运算(~~)和一元加号(+)快速转换字符串为整数
2026-11-03 22:41:12
怎么使用命令行批量格式化项目中的JS文件
2026-11-03 19:04:42
JS代码美化后报错怎么办?常见格式化冲突解决方法
2026-11-03 18:47:01
不用moment.js,怎么用原生JS Intl API格式化时间戳
2026-11-03 18:10:30
原生JS与dayjs获取当前日期的代码对比
2026-11-03 17:57:26
