protected void Button1_Click(object sender, EventArgs e) { string strName = FileUpload1.PostedFile.FileName; //使用fileupload控件获取上传文件的文件名 if (strName != "") //如果文件名存在 { bool fileOK = false; int i = strName.LastIndexOf("."); //获取。的索引顺序号,在这里。代表图片名字与后缀的间隔 string kzm = strName.Substring(i); //获取文件扩展名的另一种方法 string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower(); string newName = Guid.NewGuid().ToString(); //生成新的文件名,保证唯一性 //string xiangdui = "/UpLoadFile/";//设置文件相对网站根目录的保存路径 ,~号表示当前目录,在此表示根目录下的images文件夹 string juedui = Server.MapPath("/DownLoad/"); //设置文件保存的本地目录绝对路径,对于路径中的字符“\”在字符串中必须以“\\”表示,因为“\”为特殊字符。或者可以使用上一行的给路径前面加上@ string newFileName = juedui + newName + kzm; if (FileUpload1.HasFile) //验证 FileUpload 控件确实包含文件 { String[] allowedExtensions = { ".xls" }; for (int j = 0; j < allowedExtensions.Length; j++) { if (kzm == allowedExtensions[j]) { fileOK = true; } } } if (fileOK) { try { // 判定该路径是否存在 if (!Directory.Exists(juedui)) { Directory.CreateDirectory(juedui); } FileUpload1.PostedFile.SaveAs(newFileName);//将图片存储到服务器上 Common.ComExcelHelper ch = new Common.ComExcelHelper(); if (ch.IsHeadName(newFileName, "物流码")) { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "GetExcel('" + newName + kzm + "');", true); } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('文件格式不正确')", true); } } catch (Exception) { } } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请选择Excel文件')", true); } } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请选择Excel文件')", true); } }
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; List <Model.Promotion> list = new List <Model.Promotion>(); string type = context.Request.QueryString["type"]; BLL.Promotion bll = new BLL.Promotion(); BLL.BarCode bb = new BLL.BarCode(); switch (type) { case "one": if (context.Request.QueryString["code"] == null || context.Request.QueryString["code"] == "undefined" || context.Request.QueryString["code"] == "") { if (context.Cache["key"] != null) { list = (List <Model.Promotion>)context.Cache["key"]; IsoDateTimeConverter timeFormat = new IsoDateTimeConverter(); timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; context.Response.Write(JsonConvert.SerializeObject(list)); } } else { Model.manager adminmodel = context.Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager; Model.Promotion model = new Model.Promotion(); //model.ClientID = 0; model.UniqueID = Guid.NewGuid(); model.LogisticsCode = context.Request.QueryString["code"]; model.Operator = adminmodel.id; model.State = 3; model.BrokerageRatio = 0; model.CreateTime = Convert.ToDateTime(DateTime.Now); model.Remark = string.Empty; if (context.Cache["key"] == null) { list.Add(model); context.Cache["key"] = list; } else { list = (List <Model.Promotion>)context.Cache["key"]; list.Add(model); } try { //if (bb.ExistsOut(context.Request.QueryString["code"])) //{ if (bll.Add(model, true)) { IsoDateTimeConverter timeFormat = new IsoDateTimeConverter(); timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; context.Response.Write(JsonConvert.SerializeObject(list, timeFormat)); } //} } catch (Exception) { } } break; case "two": Common.ComExcelHelper ch = new Common.ComExcelHelper(); DataTable dt = new DataTable(); dt = ch.ReadExcel2DataTable(context.Server.MapPath("/DownLoad/") + context.Request.QueryString["filename"], "nihao", "物流码"); dt.Rows.RemoveAt(0); dt.Columns.Add("UniqueID", typeof(Guid)); //dt.Columns.Add("ClientID", typeof(Int32)); dt.Columns.Add("Operator", typeof(Int32)); dt.Columns.Add("CreateTime", typeof(DateTime)); dt.Columns.Add("State", typeof(Int32)); for (int i = 0; i < dt.Rows.Count; i++) { dt.Rows[i][1] = Guid.NewGuid(); dt.Rows[i][2] = 1; dt.Rows[i][3] = DateTime.Now; dt.Rows[i][4] = 3; if (dt.Rows[i][0] == null || dt.Rows[i][0].ToString() == "") { dt.Rows.RemoveAt(i); } } Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("UniqueID", "UniqueID"); //dic.Add("ClientID", "ClientID"); dic.Add("物流码", "LogisticsCode"); dic.Add("Operator", "Operator"); dic.Add("CreateTime", "CreateTime"); dic.Add("State", "State"); string json = ""; try { bll.Add(dt, "TempPromotion", dic); } catch (Exception) {} try { bll.Add(dt, "Promotion", dic); IsoDateTimeConverter timeFormat2 = new IsoDateTimeConverter(); int total = dt.Rows.Count; timeFormat2.DateTimeFormat = "yyyy-MM-dd HH:mm:ss"; dt = GetPagedTable(dt, Convert.ToInt32(context.Request.Form["page"]), Convert.ToInt32(context.Request.Form["rows"])); json = JsonConvert.SerializeObject(dt, new DataTableConverter(), timeFormat2); json = "{\"total\":" + "\"" + total + "\"" + ",\"rows\":" + json + "}"; } catch (Exception) { json = "{\"total\":" + "\"0\"" + ",\"rows\":[]}"; } finally { context.Response.Write(json); } break; case "Three": try { if (bb.ExistsOut(context.Request.QueryString["code"])) { context.Response.Write("true"); } else { context.Response.Write("false"); } } catch (Exception) { } break; } }