ASP.NET Web API does not allow Lengthy base64 URI

前端 未结 2 1665
野的像风
野的像风 2021-01-22 10:58

I\'m trying to receive a lengthy base64 string from my Android Client and then decode it to a bitmap in my Web API Project to be uploaded as an image to an Azure BLOB Storage. H

相关标签:
2条回答
  • 2021-01-22 11:42

    Data can be either transmitted in the URL of a request, or in the body. You are currently transmitting the data in the URL. And the URL is only allowed to be a finite length. Instead, put your base64 string in the body. See Parameter Binding in ASP.NET Web API.

    By default, simple parameters like strings are pulled from the URI. To force Web API to look for it in the body, we add a [FromBody] attribute to the parameter.

    [HttpPost]
    [Route("api/addnewpost")]
    public IHttpActionResult AddNewMediaActivity(string caption, string email,
        string type, [FromBody] string base64String)
    {
        byte[] f = Convert.FromBase64String(base64String);
        //more code........
    }
    

    And change your Android client code to put the base64 string in the body, not the URL. It's been a while since I've touched Java and I don't know what library you're using, but I imagine when you're building your HTTP request that you'll have the option of adding some data to the body of the request.

    0 讨论(0)
  • 2021-01-22 11:49

    (I have deleted the other one, I want to keep this one because the issue was using asp.net 5 beta 8 and is more recent)

    I had this issue recently and the parameter was moved to receive a byte[] (byte array) From Body

    First I tried from body and the string was received but the string was truncated and the file can't be recreated in the server side.

    In my application the base64 came from a conversion of an array buffer created from an input file.

    The base64 is sent in a json from angular.

    0 讨论(0)
提交回复
热议问题