sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

https://blog.csdn.net/weixin_37380784/article/details/81162547

jQuery插件AjaxFileUpload用来实现ajax文件上传,该插件使用非常简单,接下来写个demo演示怎么用AjaxFileUpload插件实现文件上传。

1、引入AjaxFileUpload插件相关的js

备注:测试发现,ajaxfileupload对jQuery版本是有要求的,在使用中ajaxfileupload和jQuery对应的js版本要一致,不然会导致异常发生,可以从ajaxfileupload官网下载,避免版本冲突。

2、实现上传功能代码

复制代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/base.jsp" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"😕/"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<title>ajax文件上传</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="validate/ajaxfileupload.css" />
<script type="text/javascript" src="validate/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="validate/ajaxfileupload.js" ></script>
<script type="text/javascript">
$(function(){
   //上传图片
   $("#btnUpload").click(function() {
           alert(ajaxFileUpload());
   });
});
function ajaxFileUpload() {
    // 开始上传文件时显示一个图片
    $("#wait_loading").ajaxStart(function() {
        $(this).show();
    // 文件上传完成将图片隐藏起来
    }).ajaxComplete(function() {
        $(this).hide();
    });
    var elementIds=["flag"]; //flag为id、name属性名
    $.ajaxFileUpload({
        url: 'uploadAjax.htm', 
        type: 'post',
        secureuri: false, //一般设置为false
        fileElementId: 'file', // 上传文件的id、name属性名
        dataType: 'text', //返回值类型,一般设置为json、application/json
        elementIds: elementIds, //传递参数到服务器
        success: function(data, status){  
            alert(data);
        },
        error: function(data, status, e){ 
            alert(e);
        }
    });
    //return false;
}
</script>
</head>
posted on 2022-03-05 22:21  sunny123456  阅读(94)  评论(0编辑  收藏  举报