我只想将.dat文件从Windows手机上传到php脚本。
这是我的代码,但不起作用。任何人都可以看到原因吗?
C#:
string fileBase64 = "UklGRt4lAABXQVZFZm10IBAAAAABAAEARKw...";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://...mysite.../upload.php");
request.Method = "POST";
request.ContentType = "application/octet-stream";
string postData = String.Format("file={0}", fileBase64);
// Getting the request stream.
request.BeginGetRequestStream
(result =>
{
// Sending the request.
using (var requestStream = request.EndGetRequestStream(result))
{
using (StreamWriter writer = new StreamWriter(requestStream))
{
writer.Write(postData);
writer.Flush();
}
}
// Getting the response.
request.BeginGetResponse(responseResult =>
{
var webResponse = request.EndGetResponse(responseResult);
using (var responseStream = webResponse.GetResponseStream())
{
using (var streamReader = new StreamReader(responseStream))
{
string srresult = streamReader.ReadToEnd();
}
}
}, null);
}, null);
PHP:
define("UPLOAD_DIR", "./uploads/");
if(isset($_POST['file']))
{
$base64_string = $_POST['file'];
$ifp = fopen( UPLOAD_DIR.'aaa.dat', "wb" );
fwrite( $ifp, base64_decode($base64_string) );
fclose( $ifp );
echo "ok";
echo $base64_string;
echo base64_decode($base64_string);
}else{
echo "no submit";
}
答案 0 :(得分:0)
您正在滥用HTTP协议。
这是服务器的the correct way to upload files。一定要阅读评论,你也可能想要异步这样做。对于异步路径,我建议您尝试使用C#语言的新async-await功能,与开始/结束方法相比,它对此类任务非常有用。