前台代码:
1.新增引用:
<script type="text/javascript" src="/scripts/ajaxfileupload.js"></script>2.新增控件:
<asp:FileUpload ID="fup" runat="server" Width="70"></asp:FileUpload> <input id="Button1" type="button" value="导入" onclick="Upload()"/>3.Upload()方法:
<script type="text/javascript">
function Upload() {
var file = $("#fup").val();
if(file==""){
alert("请选择上传的文件");
return;
}
$.ajaxFileUpload(
{
url: 'Handler.ashx?r=' + $('#checktime').val() + '&n=<%= regioncode %> &c=' + $('#regionname').val() + '&i=<%= id %>',//需要链接到服务器地址
secureuri: false,fileElementId: 'fup',//文件选择框的id属性
dataType: 'json',success: function (data) {
if (data.status == "success") {
alert(data.msg);
window.location.href = "inspection.aspx";
}
else {
alert(data.msg);
}
},error: function (data,status,e) {
alert(e); //就是在这弹出“语法错误”
}
}
);
}
</script>
4.Handler.ashx实现
public void ProcessRequest(HttpContext context)
{
string time = context.Request.QueryString["r"];//获取巡查时间
string regioncode = context.Request.QueryString["n"];//获取地区代码
string regionname = context.Request.QueryString["c"];//获取地区名称
string operatorid = context.Request.QueryString["i"];//获取操作人ID
context.Response.Clear();
context.Response.ContentType = "text/html";
HttpFileCollection postedFile = context.Request.Files;
//判断是否有数据
if (postedFile != null && postedFile.Count > 0)
{
int length = postedFile[0].ContentLength;//文件大小
string filename = System.IO.Path.GetFileName(postedFile[0].FileName);
//string filename = System.IO.Path.GetFileNameWithoutExtension(postedFile[0].FileName);
if (length > 0 && length < 512000)
{
string type = System.IO.Path.GetExtension(postedFile[0].FileName);
if (type == ".xls" || type == ".xlsx" || type == ".doc" || type == ".docx")
{
newland.Model.inspection inspection = new newland.Model.inspection();
string id="";
try
{
DataTable dt = WebBusiness.EvaluationManagement.inspectionDal.GetinspectionId();
id = dt.Rows[0]["id"].ToString();
inspection.ID = id;
inspection.REGIONCODE = regioncode;
inspection.REGIONNAME = regionname;
inspection.inspectionDATE = Convert.ToDateTime(time);
inspection.ATTACHMENT = filename;
inspection.OPERATOR = Convert.ToInt32(operatorid);
}
catch
{
string res = "{\"status\" : \"error\",\"msg\": \"数据库插入失败!\"}";
context.Response.Write(res);
return;
}
if (WebBusiness.EvaluationManagement.inspectionDal.Insertinspection(inspection))//插入数据库
{
try
{
string tempPath = "";
tempPath = System.Web.HttpContext.Current.Server.MapPath("/inspection/" + regionname);
if (!System.IO.Directory.Exists(tempPath))
System.IO.Directory.CreateDirectory(tempPath);//创建文件夹
postedFile[0].SaveAs(tempPath + "/" + id + type);
string res = "{\"status\" : \"success\",\"msg\": \"上传成功!\"}";
context.Response.Write(res);
}
catch
{
WebBusiness.EvaluationManagement.inspectionDal.Deteleinspection(id);
string res = "{\"status\" : \"error\",\"msg\": \"文件上传失败!\"}";
context.Response.Write(res);
return;
}
}
else
{
string res = "{\"status\" : \"error\",\"msg\": \"数据录入失败!\"}";
context.Response.Write(res);
return;
}
}
else
{
string res = "{\"status\" : \"error\",\"msg\": \"只允许上传.xls,.xlsx,.doc,.docx类型的文件!\"}";
context.Response.Write(res);
return;
}
}
else
{
string res = "{\"status\" : \"error\",\"msg\": \"文件过大!\"}";
context.Response.Write(res);
return;
}
}
}