如何转换JSON字符串

时间:2018-01-18 21:04:26

标签: asp.net json asp.net-mvc angular

[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());
    }

这是我的post方法,但由于base64字符串,似乎无法添加ctx.Items.Add(item)

 getBase64(file : File ){
         var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function () {
           // console.log(reader);
           // console.log(typeof reader.result);
            ProductService.imgstring=reader.result.split(',')[1];           
            //console.log(this.imgstring);
        }.bind(this);
        reader.onerror = function (error) {
            console.log('Error: ', error);

        };


  }

  addItem(data:any,pic:File){
   // console.log(data);
   this.getBase64(pic["0"]);
   setTimeout (() => {
        //data["picture"]=this.getBase64(pic["0"]);

        console.log(ProductService.imgstring);
        data["picture"]=ProductService.imgstring;
        console.log(data);
         let headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded');
        let reqopt = new RequestOptions({
            headers: headers
        })
        this._http.post(this._productAddUrl,JSON.stringify(data), reqopt).subscribe(function(res){
            this.response=res;
            alert(this.response);
        });

    }, 5000)

这就是我在角度方面所做的。用于传递JSON字符串。

enter image description here

我是否需要另一种转换base64字符串的方法?或者有没有办法将其包含在我的帖子方法中?

    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; }

这是 Models.Item

0 个答案:

没有答案