コード例 #1
0
        /// <summary>
        /// 删除
        /// </summary>
        public void DeleteData(string id)
        {
            var msg = new ModJsonResult();

            try
            {
                BllSysFileAttach Back = new BllSysFileAttach();
                var Model             = Back.LoadData(id);
                if (Model != null)
                {
                    string path       = Model.FilePath; //文件路径
                    string savepath   = System.Web.HttpContext.Current.Server.MapPath(path);
                    string ContractId = Model.KeyId;    //关联ID
                    string code       = Model.ModelCode;

                    int result = Back.Delete(id);
                    if (result > 0)
                    {
                        msg.success = true;
                        if (System.IO.File.Exists(savepath))
                        {
                            System.IO.File.Delete(savepath);//删除文件
                        }
                        var list = Back.QueryToAll().Where(p => p.KeyId == ContractId && p.Status == (int)StatusEnum.正常).ToList();
                        if (list.Count == 0)
                        {
                            switch (code)
                            {
                            case "InCome":    //采购入库单
                                var temp = new BllHOrderIn().LoadData(ContractId);
                                if (temp != null)
                                {
                                    temp.HasFileAttach = false;
                                    new BllHOrderIn().Update(temp);
                                }
                                break;

                            case "OutCome":    //退货单
                                var temp2 = new BllHPurchase().LoadData(ContractId);
                                if (temp2 != null)
                                {
                                    temp2.HasFileAttach = false;
                                    new BllHPurchase().Update(temp2);
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        msg.success = false;
                        msg.msg     = "操作失败";
                    }
                }
            }
            catch (Exception ex)
            {
                msg.msg = "操作失败:" + ex.Message;
            }
            WriteJsonToPage(msg.ToString());
        }
コード例 #2
0
        /// <summary>
        ///   附件上传
        /// </summary>
        public void FileUpload()
        {
            var msg = new ModJsonResult();

            try
            {
                if (Request.Files.Count > 0)
                {
                    string FileAttachPath = System.Configuration.ConfigurationManager.AppSettings["FileAttach"];//文件保存路径
                    string Dirc           = DateTime.Now.ToString("yyyy") + @"/" + DateTime.Now.ToString("MM");

                    string savepath  = System.Web.HttpContext.Current.Server.MapPath(FileAttachPath) + Dirc;//获取保存路径
                    string SmallPath = savepath + "/SmallPath/";
                    if (!Directory.Exists(savepath))
                    {
                        Directory.CreateDirectory(savepath);
                    }
                    if (!Directory.Exists(SmallPath))
                    {
                        Directory.CreateDirectory(SmallPath);
                    }
                    for (var i = 0; i < Request.Files.Count; i++)
                    {
                        HttpPostedFileBase postedFile   = Request.Files[i];
                        string             sExtension   = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.')); //获取拓展名
                        string             fileOldName  = postedFile.FileName.Substring(0, postedFile.FileName.LastIndexOf('.'));
                        string             sNewFileName = DateTime.Now.ToString(Guid.NewGuid().ToString());                    //文件新名称
                        savepath  = savepath + @"/" + sNewFileName + sExtension;                                               //保存路径
                        SmallPath = SmallPath + @"/" + sNewFileName + sExtension;                                              //保存路径
                        string message = "";
                        //var flag=UploadFileHelper.UploadSmallImg(postedFile, savepath, SmallPath, 150, 150, 500, out message);
                        //if (flag == true)
                        //{
                        postedFile.SaveAs(savepath);
                        System.IO.FileInfo fileInfo = new System.IO.FileInfo(savepath);
                        //添加附件到数据库
                        ModSysFileAttach model = new ModSysFileAttach();
                        model.Id      = Guid.NewGuid().ToString();
                        model.NameOld = fileOldName;
                        model.NameNew = sNewFileName;
                        //model.FilePath = FileAttachPath + Dirc + @"/SmallPath/" + sNewFileName + sExtension;
                        model.FilePath   = FileAttachPath + Dirc + @"/" + sNewFileName + sExtension;
                        model.FileType   = sExtension;
                        model.Extension  = sExtension;
                        model.FileSize   = new FileHelper().CountSize(fileInfo.Length);
                        model.Cid        = CurrentMaster.Cid;
                        model.CreaterId  = CurrentMaster.Id;
                        model.CreateTime = DateTime.Now;
                        model.Status     = (int)StatusEnum.正常;

                        model.KeyId     = Request["KeyId"].ToString();
                        model.ModelCode = Request["ModelCode"].ToString();

                        int result = new BllSysFileAttach().Insert(model);
                        // }
                    }

                    //修改主表附件信息
                    if (!string.IsNullOrEmpty(Request["ModelCode"].ToString()))
                    {
                        switch (Request["ModelCode"].ToString())
                        {
                        case "InCome":    //采购入库单
                            var InCome = new BllHOrderIn().LoadData(Request["KeyId"].ToString());
                            if (InCome != null)
                            {
                                InCome.HasFileAttach = true;
                                new BllHOrderIn().Update(InCome);
                            }
                            break;

                        case "OutCome":    //退货单
                            var OutCome = new BllHPurchase().LoadData(Request["KeyId"].ToString());
                            if (OutCome != null)
                            {
                                OutCome.HasFileAttach = true;
                                new BllHPurchase().Update(OutCome);
                            }
                            break;
                        }
                    }

                    msg.success = true;
                }
            }
            catch (Exception ex)
            {
                msg.success = false;
                msg.msg     = "上传失败," + ex.Message;
            }
            WriteJsonToPage(msg.ToString());
        }