Beispiel #1
0
        public ActionResult BuildProductsXML(string PIDArrStr)
        {
            if (string.IsNullOrEmpty(PIDArrStr))
            {
                Redirect("/Manager/ProductManager");
            }
            if (string.IsNullOrEmpty(PIDArrStr))
            {
                return(View());
            }
            string[] PIDArr    = PIDArrStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            int[]    tmpPIDArr = new int[PIDArr.Length];
            for (int i = 0; i < PIDArr.Length; i++)
            {
                tmpPIDArr[i] = int.Parse(PIDArr[i]);
            }
            Product[] productArr = (from product in db.Products
                                    where tmpPIDArr.Contains(product.PID)
                                    select product).ToArray();
            //生成所需的商品XML数据
            string FileName = "Product_" + Guid.NewGuid().ToString() + ".xlsx";
            string serverFP = PubFunction.GetUploadFilePathUsingDate();
            string localFP  = Server.MapPath(serverFP);

            if (!Directory.Exists(localFP))
            {
                Directory.CreateDirectory(localFP);
            }
            Dictionary <string, MemberToStringDG> dict = new Dictionary <string, MemberToStringDG>();

            dict.Add("ProductTags", new MemberToStringDG(MTSHelper.ListToString));
            dict.Add("SaleCountLST", new MemberToStringDG(MTSHelper.ListToString));
            byte[] tmpBuffer = PubFunction.SaveToExcel <Product>(productArr, dict);

            Response.Charset         = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.ContentType     = "application/octet-stream";

            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(FileName));
            Response.BinaryWrite(tmpBuffer);
            Response.Flush();
            Response.End();
            return(new EmptyResult());
        }
Beispiel #2
0
        public JsonResult Upload(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[] { '~' });
                    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));
            }
        }
Beispiel #3
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));
            }
        }