0

I am trying to upload an image to the database. My code is as follows:

I get the following error:

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

How can I get this to work ?

VIEW

    <div class="form-group">
        @Html.LabelFor(model => model.image, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.image, new { @class = "form-control", type = "file", placeholder = "image" })
            @Html.ValidationMessageFor(model => model.image)
        </div>
    </div>

MODEL

namespace MyPro.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Profile
    { 
         ...
         public byte[] image{ get; set; }
    }
}

CONTROLLER

public async Task<ActionResult> Create([Bind(Include="")] Profile profile)
{
    if (ModelState.IsValid)
    {
            var pro= new Profile{ image = profile.image};
            db.Profile.Add(pro);

            await db.SaveChangesAsync();
            return RedirectToAction("sucess");
    }
    return View(profile);
}

Database image:

enter image description here

Illep
  • 16,375
  • 46
  • 171
  • 302
  • Possible duplicate of [File upload bound to the Viewmodel](http://stackoverflow.com/questions/10757604/file-upload-bound-to-the-viewmodel) – Eugene Podskal Jan 08 '17 at 18:16
  • 1
    This may be relevant: http://stackoverflow.com/questions/11743160/how-do-i-encode-and-decode-a-base64-string I don't see any base64 encoding happening in your code. – D. Foley Jan 08 '17 at 18:16
  • 1
    could you please show the base64 encoded string you send to the server/try to save in db? Or do you actually not (want to) send the image as base64 encoded string? Then you should take a look at the .net streaming api. Either way, I think your problem is using byte[] in your model. Try using `HttpPostedFileBase` instead :) **edit**: as the other comments state. – Dominik Jan 08 '17 at 18:18
  • Sorry, but i am a newbie. Can someone give me an example please. – Illep Jan 08 '17 at 18:22
  • If I make the image in the Model `HttpPostedFileBase `, then should I change the dataType for image in my Database as well ? – Illep Jan 08 '17 at 18:24
  • this may help you out, searching for an example: http://stackoverflow.com/questions/304617/html-helper-for-input-type-file – Dominik Jan 08 '17 at 18:33
  • 1
    What does linq have to do with this? – Peter Bons Jan 08 '17 at 18:34
  • @PeterBons Not everyone is a pro. Newbie's use the forum too. :) – Illep Jan 08 '17 at 18:39
  • The error I have mentioned in my post is fired even before the Controller is been executed. Why is this ? – Illep Jan 08 '17 at 18:43
  • As I already stated, because you try to store a data type inside a byte[] when the data is not at all a byte array. Use Encoding.UTF8.GetBytes() for the setter. – D. Foley Jan 08 '17 at 18:45
  • Ok. In my database (i have added a screenshot above), what should be the `data- type` for the image field ? – Illep Jan 08 '17 at 18:50
  • When I do this in MySql it is BLOB, your mileage may vary depending on your RDBMS. – D. Foley Jan 08 '17 at 18:52
  • @D.Foley I use MSSQL. I couldn't find anything called `BLOB`. – Illep Jan 08 '17 at 18:53
  • http://stackoverflow.com/questions/15518051/save-byte-array-in-sql-server Shows how to store a blob in MSSQL. Be sure to use bound parameters when you do this. – D. Foley Jan 08 '17 at 18:54
  • Thank you. But now, i get another error `Error 5 Cannot implicitly convert type 'System.Web.HttpPostedFileBase' to 'byte[]' `. In my MSSQL DB the datatype I added was `varbinary(MAX)`, I also did generate my model, so the Datatype that was generated was `public byte[] image { get; set; }`. So I think there's a bit of a confusion between these 2 datatypes. How can I solve this ? – Illep Jan 08 '17 at 19:10

0 Answers0