Exemplo n.º 1
0
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="docId"></param>
        /// <returns></returns>
        public FileStreamResult FileDownload(string docId)
        {
            FileStreamResult Ret = null;

            try
            {
                #region 判断是否为空
                if (!string.IsNullOrEmpty(docId))
                {
                    using (TeamWorkDbContext et = new TeamWorkDbContext())
                    {
                        KeyValModel     kv   = GetCurrentStoreDirectory();
                        T_XT_Doc_Entity _doc = et.T_XT_Doc_Entity.FirstOrDefault(d => d.IsDeleted == false && d.DocId == docId);
                        if (kv != null && _doc != null)
                        {
                            string filePath = Server.MapPath(string.Format("~/DocLib/{0}/{1}", _doc.SubDirectory, _doc.InternalName));//路径
                            Ret = File(new FileStream(filePath, FileMode.Open), "text/plain", _doc.DocName);

                            _doc.DownloadCount = _doc.DownloadCount ?? 0;
                            _doc.DownloadCount++;
                            et.SaveChanges();
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
            }
            return(Ret);
        }
Exemplo n.º 2
0
        public JsonResult FilesDelete(string[] docIds)
        {
            JsonRetModel ret = new JsonRetModel {
                Ret = false
            };

            try
            {
                KeyValModel kv = GetCurrentStoreDirectory();
                if (!docIds.Any())
                {
                    ret.Msg = "未检测到需要删除的文件!";
                    return(Json(ret));
                }
                if (kv == null)
                {
                    ret.Msg = "未能获取到文件储存目录!";
                    return(Json(ret));
                }

                using (TeamWorkDbContext et = new TeamWorkDbContext())
                {
                    foreach (string _docId in docIds)
                    {
                        T_XT_Doc_Entity _doc = et.T_XT_Doc_Entity.FirstOrDefault(k => k.DocId == _docId);
                        if (_doc != null)
                        {
                            #region  除文件
                            string   path = string.Format("{0}\\{1}\\{2}", kv.Val, _doc.SubDirectory, _doc.InternalName);
                            FileInfo info = new FileInfo(path);
                            if (info.Exists)
                            {
                                info.Delete();
                            }
                            #endregion

                            #region 如果是图片类型的连带删除缩略图
                            if (CheckImageExt(_doc.DocType))
                            {
                                string   img_sp = string.Format("{0}\\{1}\\{2}_s{3}", kv.Val, _doc.SubDirectory, _doc.InternalName.Replace(_doc.DocType, ""), _doc.DocType);
                                FileInfo img_s  = new FileInfo(img_sp);
                                if (img_s.Exists)
                                {
                                    img_s.Delete();
                                }
                            }
                            #endregion

                            #region  除表数据(伪删除)
                            _doc.IsDeleted = true;
                            et.SaveChanges();
                            #endregion
                        }
                    }
                    ret.Msg = "操作成功";
                    ret.Ret = true;
                }
            }
            catch (Exception e)
            {
                ret.Msg = e.Message;
                ret.Ret = false;
            }
            return(Json(ret));
        }
Exemplo n.º 3
0
        public JsonResult FileUpload()
        {
            JsonRetModel ret = new JsonRetModel();

            try
            {
                Response.ContentType     = "text/plain";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
                Response.Charset         = "utf-8";

                string _RelevanceId    = Request["RelevanceId"],
                       _FromModuleName = Request["FromModuleName"],
                       _FromTableName  = Request["FromTableName"],
                       _ExpandOne      = Request["ExpandOne"],
                       _ExpandTwo      = Request["ExpandTwo"],
                       _ExpandThree    = Request["ExpandThree"],
                       _ExpandFour     = Request["ExpandFour"],
                       _ExpandFive     = Request["ExpandFive"];

                HttpPostedFileBase file = Request.Files["file"];

                KeyValModel kv = GetCurrentStoreDirectory();//获取存储物理路径
                if (kv != null)
                {
                    using (TeamWorkDbContext et = new TeamWorkDbContext())
                    {
                        string storeDirectoryId = kv.Key,
                               storeDirectory   = kv.Val,
                               subDirectory     = DateTime.Now.ToString("yyyyMM"),
                               uploadPath       = storeDirectory + @"\" + subDirectory + @"\";
                        bool IsImg = false;

                        if (file != null)
                        {
                            if (!Directory.Exists(uploadPath))
                            {
                                Directory.CreateDirectory(uploadPath);
                            }
                            string[] array        = file.FileName.Split('.');
                            string   suffix       = "." + array[array.Length - 1],
                                     internalName = Guid.NewGuid().ToString("N"),
                                     realPath     = uploadPath + internalName,
                                     fullPath     = realPath + suffix;

                            if (CheckImageExt(suffix))
                            {
                                IsImg = true;
                                MakeSmallImg(file.InputStream, realPath + "_s" + suffix, 100, 100);
                                MakeSmallImg(file.InputStream, fullPath, 800, 800);
                            }
                            else
                            {
                                file.SaveAs(fullPath);
                            }

                            string fileName = file.FileName.Substring(file.FileName.LastIndexOf('/') + 1);

                            T_XT_Doc_Entity _doc = new T_XT_Doc_Entity
                            {
                                DocId            = Guid.NewGuid().ToString("N"),
                                StoreDirectoryId = storeDirectoryId,
                                DocName          = fileName,
                                DocType          = suffix,
                                DocSize          = file.ContentLength,
                                SubDirectory     = subDirectory,
                                InternalName     = internalName + suffix,
                                DownloadCount    = 0,
                                FromModuleName   = _FromModuleName,
                                FromTableName    = _FromTableName,
                                RelevanceId      = _RelevanceId,
                                ExpandOne        = _ExpandOne,
                                ExpandTwo        = _ExpandTwo,
                                ExpandThree      = _ExpandThree,
                                ExpandFour       = _ExpandFour,
                                ExpandFive       = _ExpandFive,
                                CreateTime       = DateTime.Now,
                                IsDeleted        = false
                            };
                            et.T_XT_Doc_Entity.Add(_doc);
                            et.SaveChanges();

                            ret.Ret = true;
                            ret.Msg = "上传成功";

                            ret.Data = new
                            {
                                DocId      = _doc.DocId,
                                DocName    = _doc.DocName,
                                Size       = _doc.DocSize,
                                CreateTime = _doc.CreateTime,
                                Path       = string.Format("DocLib/{0}/{1}", subDirectory, _doc.InternalName)
                                             //,Path_s = IsImg?string.Format("DocLib/{0}/{1}_s{2}", subDirectory, realPath, suffix):null
                            };
                        }
                        else
                        {
                            ret.Ret = false;
                            ret.Msg = "未检测到文件";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ret.Ret = false;
                ret.Msg = ex.Message;
            }

            return(Json(ret));
        }