Jquery ajax上传文件到服务器

添加引用

<script src="/js/ajaxfileupload.js"></script>

 js文件 https://files.cnblogs.com/files/tengfei8/ajaxfileupload.js

上传附件代码:

 1 //文件上传
 2 function ajaxFileUpload() {
 3     $.ajaxFileUpload
 4     ({
 5         url: '/ApproveListHandler.aspx?Type=UpLoadAttachment', //用于文件上传的服务器端请求地址
 6         secureuri: false, //是否需要安全协议,一般设置为false
 7         fileElementId: 'txtAttachment', //文件上传域的ID
 8         dataType: 'json', //返回值类型 一般设置为json
 9         success: function (data, status)  //服务器成功响应处理函数
10         {
11             if (typeof (data.error) != 'undefined') {
12                 if (data.error != '') {
13                     $("#attTable").append("<tr class='attContent'><td class='att_title' colspan='2' >" + "附件添加失败" + "</td></tr>");
14                 } else { 
15                     var displayName = data.oldFileName;
16                     var fullname = data.fileName;
17                     allAttFullPath = allAttFullPath + fullname + "*";
18                     $("#hdAttContent").val(allAttFullPath);
19                     $("#attTable").append("<tr class='attContent'><td class='att_title'>" + displayName + "</td><td onclick='delTr(this);' class='delTd'><input type='hidden' value='" + fullname + "' /><img src='/images/selected/list_delete.png'></td></tr>");
20                       }
21             }
22         },
23         error: function (data, status, e)//服务器响应失败处理函数
24         {
25             $("#attTable").append("<tr class='attContent'><td class='att_title' colspan='2' >" + "附件添加失败,服务器响应失败,请联系管理员" + "</td></tr>");
26         }
27     });
28     return false;
29 }
View Code
原文地址:https://www.cnblogs.com/tengfei8/p/7110599.html