我正在尝试构建一个上传方法,将记录的.wav上传到我的服务器。 问题是,上传文件只是噪音而且总是一样大小。
你能告诉我为什么这是&如何解决?
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");
};
}
}
这是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";
}
}
?>
答案 0 :(得分:1)
您正在阅读和编码到Base64并始终设置相同的大小4096.您可能想尝试
int bytesRead = _data.Read(fileContent, 0, _data.Length - 1);
string b64 = Convert.ToBase64String(fileContent, 0, _data.Length);
其次URL编码你的字符串,因为base64中可以有+符号。
wc.UploadStringAsync(new Uri("http://myhost.de/t/upload.php?upload"),
"&contents=" + HttpUtility.UrlEncode(b64));
并在PHP方面
$input = base64_decode(urldecode($contents));