public string DoUpload() { int channel_id = int.Parse(Request.Form["w_comboxOutSite"]); string fileName = Request.Files[0].FileName; string filePath = ""; string fName = ""; string fExtension = ""; try { FileOperator fo = new FileOperator(fileName); fExtension = fo.Extension; if (fExtension != ".xls" && fExtension != ".xlsx") { return "{success:false,msg:'" + Resources.ProductItemMap.EXCEL_FORMAT_ERROR + "'}"; } if (!System.IO.Directory.Exists(Server.MapPath(excelPath))) { System.IO.Directory.CreateDirectory(Server.MapPath(excelPath)); } fName = DateTime.Now.ToString("yyyyMMddhhmmss") + fo.Extension; filePath = Server.MapPath(excelPath) + "/" + fName; } catch (Exception ex) { Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage(); logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message); logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; log.Error(logMessage); return "{success:false,msg:'" + Resources.ProductItemMap.EXCEL_FORMAT_ERROR + "'}"; } try { Request.Files[0].SaveAs(filePath); } catch (Exception ex) { Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage(); logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message); logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; log.Error(logMessage); } IProductItemMapExcelImplMgr ife = new ProductItemMapExcelMgr(connectionString); List<ProductItemMapCustom> pm = new List<ProductItemMapCustom>(); try { List<ProductItemMapCustom> errorPm = new List<ProductItemMapCustom>(); Resource.CoreMessage = new CoreResource("ProductItemMap"); bool isHeaderError = false; //讀取excel裡賣弄的內容 pm = ife.ReadFile(filePath, channel_id, fExtension, ref isHeaderError, errorPm); if (!isHeaderError) { if (errorPm.Count == 0) { ife.SaveToDB(pm); } else { return "{success:true,error:true,path:'" + fName + "',data:" + JsonConvert.SerializeObject(errorPm) + "}"; } } else { return "{success:true,headerError:true}"; } } catch (Exception ex) { Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage(); logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message); logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; log.Error(logMessage); return "{success:false,msg:'" + Resources.ProductItemMap.IMPORT_FAIL + "'}"; } return "{success:true,msg:'" + string.Format(Resources.ProductItemMap.IMPORT_SUCCESS_TOTAL, pm.Count) + "'}"; }
public HttpResponseBase ContinueUpload() { string fileName = Request.Params["fname"]; int channel_id = int.Parse(Request.Params["outSite"]); jsonStr = "{success:false,msg:'" + Resources.ProductItemMap.IMPORT_FAIL + "'}"; IProductItemMapExcelImplMgr ife = new ProductItemMapExcelMgr(connectionString); List<ProductItemMapCustom> pm = new List<ProductItemMapCustom>(); string filePath = Server.MapPath(excelPath) + "/" + fileName; string fExtension = "." + fileName.Split('.')[1]; try { List<ProductItemMapCustom> errorPm = new List<ProductItemMapCustom>(); bool isHeader = true; pm = ife.ReadFile(filePath, channel_id, fExtension, ref isHeader, errorPm); if (pm.Count > 0) { ife.SaveToDB(pm); } jsonStr = "{success:true,msg:'" + string.Format(Resources.ProductItemMap.IMPORT_SUCCESS_TOTAL, pm.Count) + "',data:" + JsonConvert.SerializeObject(pm) + "}"; } catch (Exception ex) { Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage(); logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message); logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; log.Error(logMessage); } this.Response.Clear(); this.Response.Write(jsonStr); this.Response.End(); return this.Response; }