Exemplo n.º 1
0
        public static Common.Core.OMSFile.Model.AttachmentInfo Upload(Common.Core.OMSFile.Model.AttachmentInfo info, bool isMutiple, TacketBase tacketBase, bool isUpdateModel = false)
        {
            IAttachmentService attachmentService = ObjectContainer.ResolveService <IAttachmentService>();

            if (info == null)
            {
                throw new ArgumentNullException("attachmentInfo", "attachmentInfo must be not null,please fix error");
            }

            if (tacketBase == null)
            {
                throw new ArgumentNullException("tacketBase", "tacketBase must be not null,please fix error");
            }

            if (info.Items != null)
            {
                if (info.Items == null)
                {
                    throw new ArgumentNullException("attachmentInfo.Items must be not null,please fix error");
                }

                foreach (var item in info.Items)
                {
                    if (item.Id.Equals(0) && (item.Content == null || item.Content.Content == null))
                    {
                        throw new ArgumentNullException("item.Content must be not null,please fix error");
                    }

                    item.UploadMode = tacketBase.Mode;
                }
            }

            var attachmentInfo = attachmentService.SaveOrUpdate(info, true, isMutiple, isUpdateModel);

            #region switch case

            if (attachmentInfo.Items == null || !attachmentInfo.Items.Any())
            {
                return(attachmentInfo);
            }

            switch (tacketBase.Mode)
            {
            case UploadMode.FTP:

                FtpTacket ftpTacket = tacketBase as FtpTacket;

                if (ftpTacket == null)
                {
                    throw new ArgumentNullException("tacketBase");
                }


                using (
                    Ftp ftp = new Ftp(ftpTacket.Host, ftpTacket.UserName, ftpTacket.PassWord,
                                      ftpTacket.Port.ToString()))
                {
                    foreach (var item in attachmentInfo.Items)
                    {
                        if (item.IsOld)
                        {
                            continue;
                        }

                        ftp.Directory = item.FilePath.Replace(Path.GetFileName(item.FilePath), string.Empty);
                        ftp.FileName  = Path.GetFileName(item.FilePath);

                        ftp.Upload(item.Content.Content);
                    }
                }

                break;

            case UploadMode.UNC:

                using (
                    Unc unc = new Unc(tacketBase.UserName, tacketBase.PassWord, tacketBase.Host))
                {
                    foreach (var item in attachmentInfo.Items)
                    {
                        //string uncDirectory = this.tacketBase.Host + "\\" + item.FilePath.Replace(Path.GetFileName(item.FilePath), string.Empty).TrimStart('/').Replace('/', '\\');
                        if (item.IsOld)
                        {
                            continue;
                        }

                        string uncFullPath = tacketBase.Host + "\\" +
                                             item.FilePath.TrimStart('/').Replace('/', '\\');

                        EnsureDirectory(uncFullPath);

                        File.WriteAllBytes(uncFullPath, item.Content.Content);
                    }
                }

                break;

            //case UploadMode.LOC:
            default:

                foreach (var item in attachmentInfo.Items)
                {
                    //string localDirectory = this.tacketBase.Host + "\\" + item.FilePath.Replace(Path.GetFileName(item.FilePath), string.Empty).TrimStart('/').Replace('/', '\\');
                    if (item.IsOld)
                    {
                        continue;
                    }

                    string localFullPath = tacketBase.Host + "\\" +
                                           item.FilePath.TrimStart('/').Replace('/', '\\');

                    EnsureDirectory(localFullPath);

                    File.WriteAllBytes(localFullPath, item.Content.Content);
                }
                break;
            }

            #endregion

            return(attachmentInfo);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 用指定的控制器上下文和绑定上下文将模型绑定到AttachmentInfo
        /// </summary>
        /// <param name="controllerContext">ControllerContext</param>
        /// <param name="bindingContext">ModelBindingContext</param>
        /// <returns>绑定后的数据</returns>
        /// <remarks>
        /// 应登明要求 重写此绑定上传方法 由原来的站点目录上传改写为FTP远程上传
        /// <para>
        /// 2014-02-13
        /// </para>
        /// <para>
        /// 唐鑫
        /// </para>
        /// </remarks>
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request    = controllerContext.HttpContext.Request;
            string          modelName  = bindingContext.ModelName;
            bool            isDb       = IsDB(request, modelName);
            bool            isMultiple = IsMultiple(request, modelName);
            bool            isRequired = IsRequired(request, modelName);
            bool            isValid    = true;

            string uploadDir = UploadDir(request, modelName);

            UploadMode     mode       = (UploadMode)IsMode(request, modelName);
            AttachmentInfo attachment = BuildAttachmentInfo(request, modelName);

            HttpFileCollectionBase     files      = controllerContext.HttpContext.Request.Files;
            IList <HttpPostedFileBase> modelFiles = files.GetMultiple(modelName)
                                                    .Where(m => !m.FileName.IsNullOrEmpty())
                                                    .ToList();

            if (!isMultiple && isRequired && (modelFiles.Count == 0 || modelFiles.Count(m => m.ContentLength > 0) == 0))
            {
                bindingContext.ModelState.AddModelError(modelName, "必填项");
                isValid = false;
            }


            #region   员工照片 做特殊处理
            if (modelFiles.Count > 0 && !string.IsNullOrEmpty(request.QueryString["empPhoto"]) && request.QueryString["empPhoto"] == "1")
            {
                if (Path.GetExtension(modelFiles[0].FileName).Trim('.').ToLower() != "jpg")
                {
                    bindingContext.ModelState.AddModelError(modelName, "仅支持如下文件格式:jpg");
                    isValid = false;
                }
                else if (modelFiles.Any(m => m.ContentLength / 1024 >= 20))
                {
                    bindingContext.ModelState.AddModelError(modelName, "超过文件大小限定:9KB ~ 20KB");
                    isValid = false;
                }
                else if (modelFiles.Any(m => m.ContentLength / 1024 < 9))
                {
                    bindingContext.ModelState.AddModelError(modelName, "超过文件大小限定:9KB ~ 20KB");
                    isValid = false;
                }
                Image pic = null;
                if (isValid)
                {
                    pic = System.Drawing.Image.FromStream(modelFiles[0].InputStream);
                }
                if (isValid && pic.Width != 358)
                {
                    bindingContext.ModelState.AddModelError(modelName, "照版的宽度必须是:358像素");
                    isValid = false;
                }
                else if (isValid && pic.Height != 441)
                {
                    bindingContext.ModelState.AddModelError(modelName, "照版的高度必须是:441像素");
                    isValid = false;
                }
            }
            #endregion

            if (modelFiles.Any(m => !SiteConfig.FileExts.Contains(Path.GetExtension(m.FileName).Trim('.').ToLower())))
            {
                bindingContext.ModelState.AddModelError(modelName, "仅支持如下文件格式:" + SiteConfig.FileExt);
                isValid = false;
            }

            if (modelFiles.Any(m => m.ContentLength > SiteConfig.MaxFileSize))
            {
                bindingContext.ModelState.AddModelError(modelName, "超过文件大小限定:" + SiteConfig.MaxFileSizeText);
                isValid = false;
            }

            if (!isValid)
            {
                return(new AttachmentInfo());
            }

            if (modelFiles.Count > 0)
            {
                if (!isMultiple)
                {
                    var items = attachment.Items.Remove();
                }

                foreach (var item in modelFiles.Select(file => BuildAttachmentItem(file, isDb, uploadDir)))
                {
                    attachment.Items.Add(item);
                }
            }

            TacketBase tacketBase = null;

            switch (mode)
            {
            case UploadMode.FTP:     //FTP远程上传
                tacketBase = new FtpTacket(SiteConfig.FtpHost, SiteConfig.FtpUsername, SiteConfig.FtpPassword);
                break;

            case UploadMode.UNC:     //共享目录上传
                tacketBase = new UncTacket(@"" + SiteConfig.UncHost, SiteConfig.UncUsername, SiteConfig.UncPassword);
                break;

            case UploadMode.LOC:     //本地上传测试
                tacketBase = new LocTacket()
                {
                    Host = SiteConfig.UploadFolderPhysicalPath
                };
                break;

            default:
                UploadMode defaultMode = (UploadMode)SiteConfig.DefaultMode;
                switch (defaultMode)
                {
                case UploadMode.FTP:         //FTP远程上传
                    tacketBase = new FtpTacket(SiteConfig.FtpHost, SiteConfig.FtpUsername,
                                               SiteConfig.FtpPassword);
                    break;

                case UploadMode.UNC:         //共享目录上传
                    tacketBase = new UncTacket(@"" + SiteConfig.UncHost, SiteConfig.UncUsername,
                                               SiteConfig.UncPassword);
                    break;

                case UploadMode.LOC:         //本地上传测试
                default:
                    tacketBase = new LocTacket()
                    {
                        Host = SiteConfig.UploadFolderPhysicalPath
                    };
                    break;
                }
                break;
            }
            //FileUploader fileUploader = new FileUploader(tacketBase);
            NewAttachmentInfo newInfo = AttachmentConvert.Convert(attachment);

            //if ((CheckFile(files) && isValid) && newInfo.Items != null && newInfo.Items.Any())
            //{
            //fileUploader.Upload(newInfo, isMultiple);
            var info = Upload(newInfo, isMultiple, tacketBase);
            //}

            attachment = AttachmentConvert.Convert(info);

            return(attachment);
        }