Ejemplo n.º 1
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            Attachment attachment;
            HttpRequestBase httpRequestBase = controllerContext.RequestContext.HttpContext.Request;
            if (!String.IsNullOrEmpty(httpRequestBase["qqfile"]))
            {
                attachment = new Attachment()
                              {
                                  ContentLength = httpRequestBase.ContentLength,
                                  ContentType = httpRequestBase.ContentType,
                                  DateAdded = DateTime.Now,
                                  FileName = httpRequestBase["qqfile"],
                                  Contents = new byte[httpRequestBase.ContentLength]
                              };
                httpRequestBase.InputStream.Read(attachment.Contents, 0, httpRequestBase.ContentLength);

            }
            else
            {
                HttpPostedFileBase @base = controllerContext.RequestContext.RouteData.Values.ContainsValue("AsyncUpload")
                                               ? httpRequestBase.Files[0]
                                               : httpRequestBase.Files[bindingContext.ModelName];
                var converter = new FileConverter();
                attachment = converter.Convert(
                    new ResolutionContext(
                        new TypeMap(new TypeInfo(typeof(HttpPostedFileWrapper)), new TypeInfo(typeof(Attachment)),MemberList.Destination),
                        @base,
                        typeof(HttpPostedFileWrapper),
                        typeof(Attachment),null));
            }
            Guid attachmentId;
            Guid.TryParse(httpRequestBase.Form[bindingContext.ModelName + "Id"], out attachmentId);
            if (attachmentId == Guid.Empty && attachment == null)
            {
                return null;
            }
            if (attachmentId != Guid.Empty && attachment.IsNull())
            {
                return new Attachment { AttachmentId = attachmentId };
            }
            if (attachmentId != Guid.Empty && attachment.IsNotNull())
            {
                attachment.AttachmentId = attachmentId;
                return attachment;
            }
            if (attachment.AttachmentId == Guid.Empty && attachment.ContentLength > 0)
            {
                return attachment;
            }
            return null;
        }
Ejemplo n.º 2
0
 protected virtual bool Equals(Attachment other)
 {
     if (ReferenceEquals(null, other))
     {
         return false;
     }
     if (ReferenceEquals(this, other))
     {
         return true;
     }
     return other.AttachmentId.Equals(this.AttachmentId) && other.ContentLength.Equals(this.ContentLength) &&
            Equals(other.ContentType, this.ContentType) && Equals(other.Contents, this.Contents) &&
            other.DateAdded.Equals(this.DateAdded) && Equals(other.FileName, this.FileName)
            && Equals(other.Tag, this.Tag) && Equals(other.Title, this.Title)
            ;
 }