/// <summary>
        /// Binds the model by using the specified controller context and binding context.
        /// </summary>
        /// <param name="controllerContext">The context within which the controller operates. The context information includes the controller, HTTP content, request context, and route data.</param>
        /// <param name="bindingContext">The context within which the model is bound. The context includes information such as the model object, model name, model type, property filter, and value provider.</param>
        /// <returns>
        /// The bound object.
        /// </returns>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="bindingContext "/>parameter is null.</exception>
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelType != typeof(HttpPostedFileBase))
            {
                throw new ArgumentException("This binder only works for HttpPostedFileBase.");
            }

            if (bindingContext.ValueProvider.GetValue(bindingContext.ModelName) != null)
            {
                var result = base.BindModel(controllerContext, bindingContext);

                if (result != null)
                {
                    return(result);
                }
            }

            if (this.AllowDataUriFiles)
            {
                var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "." + this.DataUriPropertyName);
                if (value != null)
                {
                    return(DataUriPostedFile.Parse(value.AttemptedValue));
                }
            }

            return(null);
        }
예제 #2
0
        public void When_parsing_Then_finds_right_info()
        {
            var    bytes = File.ReadAllBytes(@"..\..\Web\onebyone.bmp");
            string temp  = Convert.ToBase64String(bytes);

            var result = DataUriPostedFile.Parse(@"data:image/bmp;base64," + temp);

            Assert.That(result.ContentType, Is.EqualTo("image/bmp"));
            Assert.That(result.ContentLength, Is.EqualTo(58));
            Assert.That(result.FileName, Is.EqualTo("datauri.bmp"));
            Assert.That(result.InputStream.Length, Is.EqualTo(58));

            var newBytes = new byte[58];

            result.InputStream.Read(newBytes, 0, 58);

            bool equal = newBytes.SequenceEqual(bytes);

            Assert.That(equal, Is.True, "Read file is not the same as disk file!");

            result.SaveAs("result.bmp"); // Ensure it doesn't explode.
        }