Returning a File from a ASP.NET web api to android
<p>I have a ASP.Net web api which is intended to send a new file over the internet encoded as <code>Base64</code>. To do so, I have a public exposed REST service:</p>

<pre><code>    string imei = ControllerContext.Request.Headers.GetValues("IMEI").FirstOrDefault();
    byte[] FileAsBytes = null;
    String base64 = null;
    var svcs = new CrudService.CrudServiceClient();

    if (svcs.IsDeviceAccepted(imei))
    {
        FileAsBytes = svcs.DownloadSoftwareVersion();
    }

    if(FileAsBytes != null)
    {
         base64 = Convert.ToBase64String(FileAsBytes);
    }

    return base64;
</code></pre>

<p>and a Private service, CRUD service, which communicates directly with the database and the core layers. </p>

<p>After some debugging I found out that this service returns the correct lenght, something like <code>4194304 bytes</code>, which seems to correspond to the size of file. However on the client side, I am contacting this service inside a <code>AsyncTask</code>:</p>

<pre><code>    @Override
    protected String doInBackground(String... params) {
        Log.i("UpdateManager: ", "Inside doInBackground");

        try {

            String imei = params[0];
            String urlString = "http://10.118.18.50/Mobile.RESTPublicService/api/SoftwareUpdate";

            HttpPost post = new HttpPost(urlString);
            post.addHeader("IMEI", imei);
            response = httpClient.execute(post);

            Log.w("Update-Response: ",
                    Integer.toString(response.getStatusLine().getStatusCode())
                            + " " + response.getStatusLine());

            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

                is = response.getEntity().getContent();
                IOUtils.copy(is, writer, "UTF-8");

                _log.debug(writer.toString());

                Log.w("CONTENT: ", writer.toString());
                byte[] result = new byte[MAX_FILE_SIZE]; //7242880 b
                result = decodeBase64(writer.toString());

                Log.w("BYTE ARRAY LENGTH: ", Integer.toString(result.length));

            }

        } catch (ClientProtocolException e) {
            Log.e("Client: ", e.getMessage());
            _log.error(e.getMessage());
            return FAILED_DUE_TO_EXCEPTION;
        } catch (IOException e) {
            Log.e("IO: ", e.getMessage());
            _log.error(e.getMessage());
            return FAILED_DUE_TO_EXCEPTION;
        }
        return response.getStatusLine().toString();
    }
</code></pre>

<p>The interesting point here is the <code>result = decodeBase64(writer.toString());</code> and the decodeBase64 method:</p>

<pre><code>protected byte[] decodeBase64(String s) {
        return Base64.decode(s);
    }
</code></pre>

<p>The String <code>s</code> is what I get from the webservice:</p>

<pre><code>UEsDBBQACAgIAJteK0IAAAAAAAAAAAAAAAAZAAQAcmVzL2FuaW0vYWN0aXZpdHlmYWRlLnhtbP7KAABdkN9OwjAchc+PbW4YErnwgkRuuSGEek1MDI/SsMkmbF26es8D+AA+hs+hD6Wn0GXKab60/fq/ETK8CyCY4xNgL4TuCUPuyANZkEeyIXvySiaTqnGFbc1RO2MxHr9YU2+PbamRps5cWlmWv1ntKtNQ6ia3psqxXJbOtRul
</code></pre>

<p>This is not the whole file, but I can see that this is <code>base64 encoded</code></p>

<p>When this string is passed inti the <code>decodeBase64</code> method I only get this error message:</p>

<p><img src="http://i.stack.imgur.com/9GQsx.png" alt="enter image description here"></p>

<p>Then the application terminates. Is this the way to handle <code>Base64</code> String? Can someone suggest how I can create a file from this <code>base64</code> string? My first thought was to convert it to a <code>byte</code> array, and then create a file.  </p>
