The input is not a valid Base-64 string as it contains a non-base64 character

后端 未结 11 1755
难免孤独
难免孤独 2020-11-27 14:37

I have a REST service that reads a file and sends it to another console application after converting it to Byte array and then to Base64 string. This part works, but when th

11条回答
  •  我在风中等你
    2020-11-27 15:25

    I arranged a similar context as you described and I faced the same error. I managed to get it working by removing the " from the beginning and the end of the content and by replacing \/ with /.

    Here is the code snippet:

    var result = client.Execute(request);
    var response = result.Content
        .Substring(1, result.Content.Length - 2)
        .Replace(@"\/","/");
    byte[] d = Convert.FromBase64String(response);
    

    As an alternative, you might consider using XML for the response format:

    [WebGet(UriTemplate = "ReadFile/Convert", ResponseFormat = WebMessageFormat.Xml)]  
    public string ExportToExcel() { //... }
    

    On the client side:

    request.AddHeader("Accept", "application/xml");
    request.AddHeader("Content-Type", "application/xml");
    request.OnBeforeDeserialization = resp => { resp.ContentType = "application/xml"; };
    
    var result = client.Execute(request);
    var doc = new System.Xml.XmlDocument();
    doc.LoadXml(result.Content);
    var xml = doc.InnerText;
    byte[] d = Convert.FromBase64String(xml);
    

提交回复
热议问题