在Windows Phone 8中上传照片时,我的代码可能有什么问题?I


What could be wrong with my code in uploading a photo in windows phone 8?I

我从某处复制并粘贴了这段代码,在尝试运行它时,我收到异常错误。它有什么问题?这是我的注册.php文件

<?php
require_once("script_pages__/db_handler.php");
$db=new db_handler();
$fname=$_REQUEST['fname'];
$lname=$_REQUEST['lname'];
$username=$_REQUEST['username'];
$password=$_REQUEST['password'];
$email=$_REQUEST['email'];
$dob=$_REQUEST['dob'];

if(filter_var($email,FILTER_VALIDATE_EMAIL)){
    if(isset($_FILES['profile'])){
    $random_file_no=rand(0000,9999);
    $media=$_FILES['profile'];
    $tmpname=$_FILES['profile']['tmp_name'];
    $name=$_FILES['profile']['name'];
    $target="Uploads/";
    $profile_pic=$random_file_no.substr($username,0,3).$name;
    $target=$target.basename($profile_pic);
    move_uploaded_file( $_FILES["profile"]["tmp_name"], $target);
    $register=$db->register($fname,$lname,$username,$email,$dob,$password,$profile_pic);
    }else{
    $register=$db->register($fname,$lname,$username,$email,$dob,$password,0);
    }
echo json_encode($register);
}else{
    $response=array();
    $response["error"]=1;
    $response["message"]="Enter a valid email";
    echo json_encode($response);
}
?>

这是我的 c# 代码

void cameraCapture_Completed(object sender, PhotoResult e)
        {
            //checking if everything went fine when capturing a photo
            if (e.TaskResult != TaskResult.OK)
                return;
            string userId = "10";
            string userHash = "40a6fe73f24b4c73f0d7943c8a41adcb";
            //preparing RestRequest by adding server url, parameteres and files...
            RestRequest request = new RestRequest("http://www.imanitv.com/shareyourtrip/addpost.php", Method.POST);
            request.AddParameter("fname", Fname);
            request.AddParameter("lname", Lname);
            request.AddParameter("username", Username);
            request.AddParameter("password", Password);
            request.AddParameter("dob", Dob);
            request.AddParameter("email", Email);
            //request.AddFile(ReadToEnd(e.ChosenPhoto));
            request.AddFile("photo", 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);
                }
            });
        }
        //method for converting stream to byte[]
        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;
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            CameraCaptureTask cameraCapture = new CameraCaptureTask();
            cameraCapture.Completed += new EventHandler<PhotoResult>(cameraCapture_Completed);
            cameraCapture.Show();
        }

这是例外:

System.ArgumentNullException was unhandled by user code
  HResult=-2147467261
  Message=Value cannot be null.
Parameter name: uri
  Source=System
  ParamName=uri
  StackTrace:
       at System.UriBuilder..ctor(Uri uri)
       at RestSharp.RestClient.BuildUri(IRestRequest request)
       at RestSharp.RestClient.ConfigureHttp(IRestRequest request, IHttp http)
       at RestSharp.RestClient.ExecuteAsync(IRestRequest request, Action`2 callback, String httpMethod, Func`4 getWebRequest)
       at RestSharp.RestClient.ExecuteAsync(IRestRequest request, Action`2 callback)
       at RestSharp.RestClientExtensions.ExecuteAsync(IRestClient client, IRestRequest request, Action`1 callback)
       at RestSharpTrial.MainPage.cameraCapture_Completed(Object sender, PhotoResult e)
       at Microsoft.Phone.Tasks.ChooserBase`1.FireCompleted(Object sender, TTaskEventArgs e, Delegate fireThisHandlerOnly)
       at Microsoft.Phone.Tasks.CameraCaptureTask.OnInvokeReturned(Byte[] outputBuffer, Delegate fireThisHandlerOnly)
       at Microsoft.Phone.Tasks.GenericChooser.OnInvokeReturned(Byte[] outputBuffer, Delegate d)
       at Microsoft.Phone.Tasks.ChooserListener.OnChildTaskReturned(ChildTaskReturnedEventArgs args)
       at Microsoft.Phone.TaskModel.Interop.Task.FireOnChildTaskReturned(IntPtr returnDataPtr, UInt32 returnDataSize)
  InnerException: 

RestSharp 现在要求你将 RestClient 的 BaseUrl 设置为 Uri。此外,它不再允许在 RestRequest 中定义完整的 url。有一个关于此的错误报告,该错误已关闭,并带有注释,指示它旨在用于在客户端中设置基本URL,然后请求将相对于该客户端进行重用 https://github.com/restsharp/RestSharp/issues/606。

修复代码 将请求更改为相对路径

RestRequest request = new RestRequest("shareyourtrip/addpost.php", Method.POST);

然后在创建客户端后,设置基本 URL

RestClient restClient = new RestClient();
restClient.BaseUrl = new Uri("http://www.imanitv.com");