1

Is there any possible way we can convert it? I pretty much ran out of ideas on how to do it. Any help will be much appreciated. Thanks!

[System.Web.Mvc.HttpPost]
    public ActionResult addItem()
    {
        var tae = Request.Form[0];  
        var ctx = new Models.CangsODEntities4();

        Models.Item item = Newtonsoft.Json.JsonConvert.DeserializeObject<Models.Item>(tae);


            ctx.Items.Add(item);
            ctx.SaveChanges();
            Response.StatusCode = 200; 

            return Content(item.itemID.ToString());

    }

This is my POST method for adding an item.

    public int itemID { get; set; }
    public string itemName { get; set; }
    public int itemQuantityStored { get; set; }
    public decimal itemPrice { get; set; }
    public int purchaseCount { get; set; }
    public byte[] picture { get; set; }
    public int isDeleted { get; set; }

This is how my Models.Item looks like.

This is the JSON being passed

tad00
  • 51
  • 1
  • 7
  • Convert it to what ? – Shyju Jan 20 '18 at 20:31
  • To what do you want to convert the base64 string? What does your code look like? – Max Jan 20 '18 at 20:31
  • @Shyju converting it into an image/blob so that I can store in my database. – tad00 Jan 20 '18 at 20:36
  • 2
    Then look at this: https://stackoverflow.com/questions/6733845/c-sharp-convert-a-base64-byte – jAC Jan 20 '18 at 20:38
  • Use a view model and leverage model binding (No need to explicitly deserialize it in your action method). You can use `HttpPostedFileBase` for the uploaded file. You can convert the file to byte array and store it, – Shyju Jan 20 '18 at 20:44
  • @jAC yup but how do i get the base64 string from the json string? – tad00 Jan 20 '18 at 20:48
  • @tad00 Use a JSON parser and extract the value by key. There're thousand of questions here on SO. Do some research, please. Or parse it into a class instance (object) -> deserialization. (https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp, https://stackoverflow.com/questions/29841503/json-serialization-deserialization-in-asp-net-core) – jAC Jan 20 '18 at 20:49
  • You need to show us your JSON, and point out which property on your Models.Item is a Base64 string. Converting JSON to objects is trivial. Converting Base64 strings to human readable strings is also trivial. From your question it is impossible to figure out where your problem lies. Also Request.Form[0] - this is an awful way to get data from your browser to the server. You should look at proper model binding by adding a parameter to your action method, and allowing model binding to do its thing. – Ian Robertson Jan 20 '18 at 20:52
  • @IanRobertson sorry for that, Im still new to this. I already uploaded the image of the JSON being passed. – tad00 Jan 20 '18 at 21:01
  • Since you are using `DeserializeObject()`, your Item's `picture` property should be `string` instead of `byte[]`. Then you can access the value with `item.picture` then convert that as required. If Item is used for other purposes and you cannot alter the `picture` type, then define a new class with the sole purpose of marshalling the JSON data. – Jasen Jan 20 '18 at 21:19
  • What is the SQL data type of the column you want the image saved to? – SBFrancies Jan 21 '18 at 01:33
  • @Jasen Yup! Thats what I did but when I deserialize it, this error comes out: *Invalid length for a Base64 char array*. So that is why I wasn't able to access the value. – tad00 Jan 21 '18 at 13:43
  • @SBFrancies the data type is image. – tad00 Jan 21 '18 at 13:44
  • @Jasen *Correction: The error is: *Invalid length for a Base64 char array or string* – tad00 Jan 21 '18 at 14:16

0 Answers0