예제 #1
0
        public JsonResult UploadProductsExcel(HttpPostedFileBase fileData)
        {
            if (fileData != null)
            {
                try
                {
                    // 文件上传后的保存路径
                    string tmpStr = "";
                    if (Request.Params.AllKeys.Contains("toPath"))
                    {
                        tmpStr = Request.Params["toPath"].ToString();
                    }
                    else
                    {
                        tmpStr = PubFunction.GetUploadFilePathUsingDate();
                    }
                    string filePath = Server.MapPath("/Uploads/");
                    if (!string.IsNullOrEmpty(tmpStr))
                    {
                        filePath = Server.MapPath(tmpStr);
                    }
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    string fileName      = Path.GetFileName(fileData.FileName);       // 原始文件名称
                    string fileExtension = Path.GetExtension(fileName);               // 文件扩展名
                    string saveName      = Guid.NewGuid().ToString() + fileExtension; // 保存文件名称

                    fileData.SaveAs(filePath + saveName);
                    tmpStr = tmpStr.TrimStart(new char[] { '~' });

                    //处理上传的Excel文件
                    Dictionary <string, StringToMemberDG> dict = new Dictionary <string, StringToMemberDG>();
                    dict.Add("ProductTags", new StringToMemberDG(MTSHelper.ListToString));
                    dict.Add("SaleCountLST", new StringToMemberDG(MTSHelper.ListToString));
                    IEnumerable <Product> productArr = PubFunction.LoadFromExcel <Product>(filePath + saveName, dict);
                    if (productArr == null)
                    {
                        //文件不存在
                    }
                    else
                    {
                        foreach (Product item in productArr)
                        {
                            db.Products.Add(item);
                        }
                        db.SaveChanges();
                    }

                    return(Json(new { Success = true, FileName = fileName, SaveName = saveName, imgSrc = tmpStr + saveName }));
                }
                catch (Exception ex)
                {
                    return(Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet));
                }
            }
            else
            {
                return(Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet));
            }
        }