什么是php代码检索文件发送使用windows phone 8 Restsharp
本文关键字:windows phone Restsharp php 代码 检索 文件 什么 | 更新日期: 2023-09-27 18:12:36
这里是restsharp代码,请帮助我检索发送的文件的PHP代码,如果它是在第一个地方发送,我真的不知道。请告诉我,如果我的c#代码是正确的,它真的发送文件或不。代码成功发送了参数,但对于文件我不知道,我不能告诉,直到我有一个坚实的PHP代码,可以处理请求。我从http://nediml.wordpress.com/2012/05/10/uploading-files-to-remote-server-with-multiple-parameters/获取了代码问候和感谢
private void Button_Click(object sender, RoutedEventArgs e)
{
CameraCaptureTask cameraCapture = new CameraCaptureTask();
cameraCapture.Completed += cameraCapture_Completed;
cameraCapture.Show();
}
private void cameraCapture_Completed(object sender, PhotoResult e)
{
if (e.TaskResult != TaskResult.OK)
return;
string userId = tb.Text;
//preparing RestRequest by adding server url, parameteres and files...
RestRequest request = new RestRequest("http://server/page.php", Method.POST);
request.AddParameter("id", userId);
request.AddFile("image", ReadToEnd(e.ChosenPhoto), "photo.jpg", "image/pjpeg");
//calling server with restClient
RestClient restClient = new RestClient();
restClient.ExecuteAsync(request, (response) =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
//upload successfull
MessageBox.Show("Upload completed succesfully...'n" + response.Content);
}
else
{
//error ocured during upload
MessageBox.Show(response.StatusCode + "'n" + response.StatusDescription);
}
});
}
public byte[] ReadToEnd(System.IO.Stream stream)
{
long originalPosition = stream.Position;
stream.Position = 0;
try
{
byte[] readBuffer = new byte[4096];
int totalBytesRead = 0;
int bytesRead;
while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
{
totalBytesRead += bytesRead;
if (totalBytesRead == readBuffer.Length)
{
int nextByte = stream.ReadByte();
if (nextByte != -1)
{
byte[] temp = new byte[readBuffer.Length * 2];
Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
readBuffer = temp;
totalBytesRead++;
}
}
}
byte[] buffer = readBuffer;
if (readBuffer.Length != totalBytesRead)
{
buffer = new byte[totalBytesRead];
Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
}
return buffer;
}
finally
{
stream.Position = originalPosition;
}
}
对于PHP端,您可以使用以下代码(这将是page.php的内容)来检查服务器接收到的内容:
<?php
echo '<pre>';
// show details of file(s) sent to page.
var_dump($_FILES);
// show details of parameters POSTed to page.
var_dump($_POST);
echo '</pre>';
?>
这允许您检查从c#端是否发生了。
如果你不能直接查看服务器的输出,你可以输出到一个文件(details.txt)在服务器上与page.php相同的文件夹中,使用下面的代码:
<?php
file_put_contents('details.txt', print_r($_FILES, true) . "'n" . print_r($_POST, true));
?>
你应该仔细阅读$_FILES超全局变量和在PHP中处理文件上传。