You must Sign In to post a response.
  • Category: ASP.Net MVC

    Saving Image as blob in database

    i want to save Image directly into database instead of file system.for that i am using blob type i.e varbinary(max) in from upload.ashx i am passing that file into byte[] to js file
    in upload.ashx i have property defined for byte as,
    private int? _DocumentSize;
    public int? DocumentSize
    get { return _DocumentSize; }
    set { _DocumentSize = value; }
    public byte[] _DocumentImage;
    public byte[] DocumentImage
    get { return _DocumentImage; }
    set { _DocumentImage = value; }
    n code is as below...
    UploadFile FileObject = new UploadFile();
    //new code
    byte[] fileData = null;

    byte[] bytes;

    bytes = new byte[PostedFile.ContentLength];
    FileObject.DocumentImage = bytes;

    FileObject.DocumentTypeId = DocumentTypeId;
    FileObject.DocumentSize = filesize;

    string FileObjectString = new JavaScriptSerializer().Serialize(FileObject);

    but it is throwong expception that --
    {System.InvalidOperationException: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
    at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)
    at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)
    at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj)
    at Upload.ProcessRequest(HttpContext context) in d:\TestLoop30ForChanges\Project\Upload.ashx:line 156}

    what is the issue in code
    or how should i do it.
    any solution ??
  • #765444

    I think JavaScriptSerializer Issue

    set your property in top place use this code


    Property here

    Name : Dotnet Developer-2015
    Email Id :

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #765453
    You could use a byte[] on your model and a HttpPostedFileBase on your view model
    see below
    //in MyViewModel
    public class MyViewModel
    public HttpPostedFileBase File { get; set; }

    public class HomeController: Controller
    public ActionResult Index()
    var model = new MyViewModel();
    return View(model);

    public ActionResult Index(MyViewModel model)
    if (!ModelState.IsValid)
    return View(model);

    byte[] uploadedFile = new byte[model.File.InputStream.Length];
    model.File.InputStream.Read(uploadedFile, 0, uploadedFile.Length);

    // now you could pass the byte array to your model and store wherever
    // you intended to store it

    return Content("Thanks for uploading the file");

    //finally your view
    @model MyViewModel
    @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
    < div>
    @Html.LabelFor(x => x.File)
    @Html.TextBoxFor(x => x.File, new { type = "file" })
    @Html.ValidationMessageFor(x => x.File)
    < /div>

    < button type="submit">Upload</button>

    Hope it helps

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765489
    The below url is best example for blob

    Dipti Choudhari

  • #765514
    Thanks for all replies.The issue was with javascript serializer.i need to use json serializer instead.

  • #765531
    You can set the MaxJsonLength property on your web.config to limit upload file size as follows://default 102400 i.e. 100kb
    <jsonSerialization maxJsonLength="20000000"/>

  • Sign In to post your comments