Exemple #1
0
        /// <summary>
        /// 处理旧图片
        /// </summary>
        /// <param name="strFileSavePath">图片虚拟路径</param>
        /// <returns></returns>
        public CommonResult OldImageHandler(string strFileSaveVirtualPath, ImageUploadBaseConfig imageUploadConfig, string strUserName)
        {
            try
            {
                string strFileSavePath = PathHelper.GetMapPath(strFileSaveVirtualPath);
                //是否存在图片文件
                if (FileHelper.Exists(strFileSavePath))
                {
                    #region 处理同路径旧图片
                    //File.Delete(strFileSavePath);
                    string strNewFileName = FileHelper.ResetFileMinName(strFileSavePath);
                    if (!FileHelper.RenameFile(strFileSavePath, strNewFileName))                  //重命名旧文件
                    {
                        return(CommonResult.Instance("旧文件重命名失败"));
                    }
                    //更新旧文件的信息
                    Document oldDocumentModel = new Document();
                    oldDocumentModel.UID          = AllServices.DocumentService.GetUID(strFileSaveVirtualPath);
                    oldDocumentModel.DocumentName = strNewFileName;
                    oldDocumentModel.DocumentPath = string.Format("{0}/{1}", imageUploadConfig.SavePath, strNewFileName);
                    oldDocumentModel.Editor       = strUserName;
                    oldDocumentModel.EditTime     = DateTime.Now;
                    oldDocumentModel.State        = Convert.ToInt32(ItemState.Disable);

                    #endregion
                    return(AllServices.DocumentService.Update(oldDocumentModel));
                }
                else
                {
                    return(CommonResult.Instance("处理旧图片失败"));
                }
            }
            catch (Exception ex)
            {
                return(CommonResult.Instance(ex.ToString()));
            }
        }
Exemple #2
0
        /// <summary>
        /// 上传图片合并处理
        /// </summary>
        /// <param name="file"></param>
        /// <param name="imageUploadConfig">图片上传配置基类或其派生类</param>
        /// <returns></returns>
        public CommonResult ChunkImageHandler(ImageUploadBaseConfig imageUploadConfig, string strUserName, DataModel dataModel, ActionType actionType)
        {
            try
            {
                //验证目标目录是否存在
                if (!Directory.Exists(PathHelper.GetMapPath(imageUploadConfig.SavePath)))
                {
                    return(CommonResult.Instance("找不到指定的上传文件目录"));
                }
                string strFileName = dataModel["DefinedName"].ToStr();
                string strExt      = dataModel["Ext"].ToStr();
                //设置图片存放目录(虚拟路径)
                string strFileSaveVirtualPath = string.Format("{0}/{1}{2}", imageUploadConfig.SavePath, strFileName, strExt);
                //设置图片存放目录(物理路径)
                string strFileSavePath = PathHelper.GetMapPath(strFileSaveVirtualPath);

                //处理相同路径图片
                this.OldImageHandler(strFileSaveVirtualPath, imageUploadConfig, strUserName);

                #region 合并文件
                string strGuid = dataModel["Guid"].ToStr();
                //设置图片存放分片目录(虚拟路径)
                string strFileSaveChunkPath = string.Format("{0}/{1}", imageUploadConfig.SavePath, "chunk\\" + strGuid + "\\");
                string strSourcePath        = PathHelper.GetMapPath(strFileSaveChunkPath); //临时文件夹
                string strTargetPath        = strFileSavePath;                             //合并后的文件

                DirectoryInfo dicInfo = new DirectoryInfo(strSourcePath);
                if (Directory.Exists(Path.GetDirectoryName(strSourcePath)))
                {
                    FileInfo[] files = dicInfo.GetFiles();
                    foreach (FileInfo file in files.OrderBy(f => int.Parse(f.Name)))
                    {
                        FileStream   addFile   = new FileStream(strTargetPath, FileMode.Append, FileAccess.Write);
                        BinaryWriter AddWriter = new BinaryWriter(addFile);

                        //获得上传的分片数据流
                        Stream       stream     = file.Open(FileMode.Open);
                        BinaryReader TempReader = new BinaryReader(stream);
                        //将上传的分片追加到临时文件末尾
                        AddWriter.Write(TempReader.ReadBytes((int)stream.Length));
                        //关闭BinaryReader文件阅读器
                        TempReader.Close();
                        stream.Close();
                        AddWriter.Close();
                        addFile.Close();

                        TempReader.Dispose();
                        stream.Dispose();
                        AddWriter.Dispose();
                        addFile.Dispose();
                    }
                    FileHelper.DelectDir(strSourcePath);
                    CommonResult commonResult = this.AddImageHandler(0, strFileName, strFileSaveVirtualPath, dataModel["Ext"].ToStr(), dataModel["Size"].ToStr(), dataModel["DpiID"].ToInt32(), actionType, strUserName);
                    return(CommonResult.Instance());
                }
                return(CommonResult.Instance());
            }
            catch (Exception ex)
            {
                return(CommonResult.Instance(ex.ToString()));
            }
            #endregion
        }
Exemple #3
0
        /// <summary>
        /// 上传图片处理
        /// </summary>
        /// <param name="file"></param>
        /// <param name="imageUploadConfig">图片上传配置基类或其派生类</param>
        /// <returns></returns>
        public CommonResult ImageHandler(HttpPostedFileBase postFile, ImageUploadBaseConfig imageUploadConfig, string strUserName, DataModel dataModel, ActionType actionType)
        {
            try
            {
                #region 验证并保存原图
                //验证目标目录是否存在
                if (!Directory.Exists(PathHelper.GetMapPath(imageUploadConfig.SavePath)))
                {
                    return(CommonResult.Instance("找不到指定的上传文件目录"));
                }
                //获取文件扩展名
                string fileExtension = Path.GetExtension(postFile.FileName);
                //检查文件是否为可上传的文件格式
                bool isAllowExtension = imageUploadConfig.AllowExtension.ToStr().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Contains(fileExtension);
                if (!isAllowExtension)
                {
                    return(CommonResult.Instance("不允许上传的图片类型"));
                }
                //上传文件大小限制
                if (postFile.ContentLength > imageUploadConfig.MaxSize * 1024)
                {
                    return(CommonResult.Instance(string.Format("上传的图片不能大于{0}K", imageUploadConfig.MaxSize)));
                }
                //设置图片名称
                var fileName = dataModel["DefinedName"].ToStr();                //postFile.FileName;
                //取得扩展名
                string strExt = dataModel["Ext"].ToStr();
                //取得图片名称(去扩展名)
                string strFileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
                //设置图片存放目录(虚拟路径)
                string strFileSaveVirtualPath = string.Format("{0}/{1}{2}", imageUploadConfig.SavePath, fileName, strExt);
                //设置图片存放目录(物理路径)
                string strFileSavePath = PathHelper.GetMapPath(strFileSaveVirtualPath);

                //是否处理相同路径图片
                this.OldImageHandler(strFileSaveVirtualPath, imageUploadConfig, strUserName);
                //if(FileHelper.Exists(strFileSavePath))
                //{
                //	#region 处理同路径旧图片
                //	//File.Delete(strFileSavePath);
                //	string strNewFileName = FileHelper.ResetFileMinName(strFileSavePath);
                //	if(!FileHelper.RenameFile(strFileSavePath,strNewFileName))//重命名旧文件
                //	{
                //		return CommonResult.Instance("旧文件重命名失败");
                //	}
                //	//更新旧文件的信息
                //	Document oldDocumentModel = new Document();
                //	oldDocumentModel.UID = AllServices.DocumentService.GetUID(strFileSaveVirtualPath);
                //	oldDocumentModel.DocumentName = strNewFileName;
                //	oldDocumentModel.DocumentPath = string.Format("{0}/{1}",imageUploadConfig.SavePath,strNewFileName);
                //	oldDocumentModel.Editor = stringUserName;
                //	oldDocumentModel.EditTime = DateTime.Now;
                //	oldDocumentModel.State = Convert.ToInt32(ItemState.Disable);
                //	AllServices.DocumentService.Update(oldDocumentModel);
                //	#endregion

                //}
                //保存图片
                postFile.SaveAs(strFileSavePath);
                #endregion

                #region 处理图片
                string mode    = imageUploadConfig.Mode;
                string strJson = string.Empty;

                if (!mode.Contains("None"))
                {
                    //处理后的图片保存目录
                    string imgHandlerDir = imageUploadConfig.SavePath + "/" + "m";
                    if (!Directory.Exists(PathHelper.GetMapPath(imgHandlerDir)))
                    {
                        Directory.CreateDirectory(PathHelper.GetMapPath(imgHandlerDir));
                    }
                    //目标图片名称
                    string fileNameByTarget = Path.GetFileName(strFileSavePath);
                    //目标图片的虚拟路径
                    string fileSaveVirtualPathByTarget = string.Format("{0}/{1}", imgHandlerDir, fileNameByTarget);
                    //目标图片的物理路径
                    string fileSavePathByTarget = PathHelper.GetMapPath(fileSaveVirtualPathByTarget);
                    //处理图片
                    ImageHelper.CompressType cType = ImageHelper.CompressType.Zoom;
                    switch (mode.ToLower())
                    {
                    case "cut":
                        cType = ImageHelper.CompressType.WidthAndHeightCut;
                        break;

                    case "zoom":
                        cType = ImageHelper.CompressType.Zoom;
                        break;

                    case "stretch":
                        cType = ImageHelper.CompressType.WidthAndHeight;
                        break;
                    }
                    ImageHelper.Thumb(strFileSavePath, fileSavePathByTarget, imageUploadConfig.Width, imageUploadConfig.Height, cType, imageUploadConfig.Quality);
                    //添加上传文件记录(处理后的图片)
                    AllServices.DocumentService.Add(new Models.Document()
                    {
                        //DocumentName = fileNameByTarget,
                        ////File_Extension = fileExtension.ToLower(),
                        //DocumentPath = fileSaveVirtualPathByTarget,
                        //PictureSize = new System.IO.FileInfo(fileSavePathByTarget).Length.ToStr(),
                        ////IsSystemCreate = true
                    });
                    //删除原图
                    if (imageUploadConfig.DelOriginalPhoto)
                    {
                        File.Delete(strFileSavePath);
                    }
                    else
                    {
                        //添加上传文件记录(原图)
                        AllServices.DocumentService.Add(new Models.Document()
                        {
                            //DocumentName = fileName,
                            //File_Extension = fileExtension.ToLower(),
                            //DocumentPath = strFileSaveVirtualPath,
                            //PictureSize = postFile.ContentLength.ToStr(),
                            //IsSystemCreate = false
                        });
                    }
                    strFileSaveVirtualPath = fileSaveVirtualPathByTarget;
                }
                #endregion

                //释放图片资源
                postFile.InputStream.Close();
                postFile.InputStream.Dispose();

                CommonResult commonResult = this.AddImageHandler(0, fileName, strFileSaveVirtualPath, dataModel["Ext"].ToStr(), dataModel["Size"].ToStr(), dataModel["DpiID"].ToInt32(), actionType, strUserName);
                return(commonResult);
            }
            catch (Exception ex)
            {
                return(CommonResult.Instance(ex.ToString()));
            }
        }
Exemple #4
0
        /// <summary>
        /// 图片下载处理(单个图片直接返回,多个图片打包成zip,成功返回压缩包地址)
        /// </summary>
        /// <param name="postFile"></param>
        /// <param name="imageUploadConfig"></param>
        /// <param name="stringUserName"></param>
        /// <param name="dataModel"></param>
        /// <param name="actionType"></param>
        /// <returns></returns>
        public CommonResult ImageHandler(ImageUploadBaseConfig imageUploadConfig, List <DataModel> listDataModel)
        {
            //验证目标目录是否存在
            if (!Directory.Exists(PathHelper.GetMapPath(imageUploadConfig.SavePath)))
            {
                return(CommonResult.Instance("找不到指定的上传文件目录"));
            }
            if (!Directory.Exists(PathHelper.GetMapPath(imageUploadConfig.SaveCompressPath)))
            {
                return(CommonResult.Instance("找不到指定的存放压缩文件目录"));
            }
            if (!Directory.Exists(PathHelper.GetMapPath(imageUploadConfig.SaveTempPath)))
            {
                return(CommonResult.Instance("找不到指定的存放打包文件临时目录"));
            }


            if (listDataModel.Count() == 1)           //单个文件下载,不需要打包
            {
                DataModel dataModel = listDataModel[0];
                string    strUrl    = dataModel["url"].ToStr();
                if (string.IsNullOrEmpty(strUrl))               //设置图片存放目录(物理路径)
                {
                    return(CommonResult.Instance("下载链接为空,请检查"));
                }
                string strSourceFileName = PathHelper.GetMapPath(strUrl);
                return(CommonResult.Instance(2, null, strSourceFileName));
            }
            //要压缩的文件夹,把需要打包的文件存放在此文件夹
            string strPath = PathHelper.GetMapPath(imageUploadConfig.SaveTempPath);

            FileHelper.DelectDir(strPath);
            try
            {
                //压缩后的文件存放路径
                string strDestFile     = PathHelper.GetMapPath(imageUploadConfig.SaveCompressPath);
                string strName         = "icon.zip";
                string strLastDestFile = string.Format("{0}/{1}", strDestFile, FileHelper.ResetFileName(strName));

                foreach (DataModel dataModel in listDataModel)
                {
                    string strUrl            = dataModel["url"].ToStr();
                    string strSourceFileName = PathHelper.GetMapPath(strUrl);
                    string strFileName       = Path.GetFileName(strSourceFileName);
                    string strNewPath        = string.Format("{0}/{1}", strPath, strFileName);
                    System.IO.File.Copy(strSourceFileName, strNewPath, true);                  //拷贝文件到压缩文件夹  //true为覆盖同名文件
                }
                ZipUtil.CreateZipFile(strPath, strLastDestFile);
                //单个文件下载,不需要打包
                //foreach(string fileName in list)
                //{
                //	string sourceFileName = System.IO.Path.Combine(filePath,fileName) + ".xls";
                //	string destFileName = System.IO.Path.Combine(dPath,fileName) + ".xls";
                //	System.IO.File.Copy(sourceFileName,destFileName,true);
                //}
                //参数为文件存放路径,下载的文件格式,文件名字
                //return File(destFile,"application/image",System.IO.Path.GetFileName(destFile));

                return(CommonResult.Instance(1, null, strLastDestFile));
            }
            catch (Exception ex)
            {
                FileHelper.DelectDir(strPath);
                return(CommonResult.Instance(ex.ToString()));
            }
        }