IMX
IMX

Reputation: 3652

Uploaded audio only short noise

I am trying to build an uploading-method, which uploads a recorded .wav to my server. The problem is, that the uploading file is just noise and has always the same size.

Can you tell me why this is & how to to fix it?

private void upload()
{
    var isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
    if (isoStore.FileExists("AudioTest.wav"))
    {
        System.IO.IsolatedStorage.IsolatedStorageFileStream _data =
            isoStore.OpenFile("AudioTest.wav", FileMode.Open);

        byte[] fileContent = new byte[_data.Length];
        int bytesRead = _data.Read(fileContent, 0, 4096);

        string b64 = Convert.ToBase64String(fileContent, 0, 4096);

        WebClient wc = new WebClient();
        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        wc.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");

        wc.UploadStringAsync(new Uri("http://myhost.de/t/upload.php?upload"),
            "&contents=" + b64);

        wc.UploadStringCompleted += (a, b) =>
        {
            Debug.WriteLine("Upload done");
        };
    }
}

and this is the PHP-Side

<?php
    echo "Starting upload\n";

    if(isset($_GET["upload"]))
    {
        $contents = $_POST["contents"];
        echo $filename;
        try
        {
            $file = fopen("filename.wav", "w");
            $input = base64_decode($contents);
            fwrite($file, $input);
            fclose($file);
        }
        catch (Exception $e) 
        {
            echo 'Caught exception: ',  $e->getMessage(), "\n";
        }
    }
?>

Upvotes: 1

Views: 82

Answers (1)

Adam
Adam

Reputation: 16199

You are reading and encoding to Base64 and always setting the same size 4096. You might want to try

int bytesRead = _data.Read(fileContent, 0, _data.Length - 1);

string b64 = Convert.ToBase64String(fileContent, 0, _data.Length);

Secondly URL Encode your string due to the fact that base64 can have + signs in it.

wc.UploadStringAsync(new Uri("http://myhost.de/t/upload.php?upload"),
        "&contents=" + HttpUtility.UrlEncode(b64));

and on the PHP side

$input = base64_decode(urldecode($contents));

Upvotes: 1

Related Questions