如何使用MyWebRequest将图像从c#app上传到服务器(php文件)

如何使用MyWebRequest将图像从c#app上传到服务器(php文件),c#,php,winforms,webrequest,C#,Php,Winforms,Webrequest,我使用MyWebRequest类将信息从c#应用程序发布到服务器,并连接到php文件 这就是MyWebRequest类 class MyWebRequest { private WebRequest request; private Stream dataStream; private string status; public String Status { get

我使用MyWebRequest类将信息从c#应用程序发布到服务器,并连接到php文件

这就是MyWebRequest类

    class MyWebRequest
    {
        private WebRequest request;
        private Stream dataStream;

        private string status;

        public String Status
        {
            get
            {
                return status;
            }
            set
            {
                status = value;
            }
        }

        public MyWebRequest(string url)
        {
            request = WebRequest.Create(url);
        }

        public MyWebRequest(string url, string method)
            : this(url)
        {

            if (method.Equals("GET") || method.Equals("POST"))
            {
                request.Method = method;
            }
            else
            {
                throw new Exception("Invalid Method Type");
            }
        }

        public MyWebRequest(string url, string method, string data)
            : this(url, method)
        {
            string postData = data;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

        }

        public string GetResponse()
        {
            WebResponse response = request.GetResponse();
            this.Status = ((HttpWebResponse)response).StatusDescription;
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();
            return responseFromServer;
        }
    }
以C形式:

在csharp.php文件中:

if ($_POST["Metod"] == 'Uploadfiles'){

                if ($_POST["Uploadtype"] == 'image')
                {
                    $uploadDirectory = "./uploads/";
                    $pathname = $_POST["Pathname"];
                    $fileName = $_POST["Filename"];
                    $uploadPath = $uploadDirectory . $fileName; 
                    $didUpload = move_uploaded_file($pathname, $uploadPath);
                    if ($didUpload)
                    {
                        echo "Sucsess";
                    }
                    else
                    {
                        echo "Erro In Upload Imgae.";
                    }
                }
            }
我在c#中检查大小,但在加载表单并按upload btn后:

显示:上传图片时出错。按摩


我在其他表单中使用MyWebRequest类,并且工作正常

您不会将
image1
字节传递给
MyWebRequest
类,并且不会将文件发送到远程服务器。php代码似乎接受文件路径作为参数,但如果文件位于网站根文件夹下,并且客户端代码(c#form)在托管web应用的同一服务器上执行,则它将起作用。您还使用了
move_uploaded_file
错误。这不适用于任何作为第一个参数抛出的任意文件路径-它首先检查文件是否确实是由文件上载产生的。您需要使用PHP在$\u文件数组中提供的临时路径。我的应用程序没有主机,数据库处于脱机状态,我只想联机保存图像。我只是在线检查用户名,这与mywebrequest类正常工作。@polrealestate您刚才所说的没有任何相关性,也没有对上面的评论形成任何有用的回应。我建议您找到一些教程/示例,向您展示a)如何使用C#和b)如何使用PHP接收文件。两者都是共同的任务。你应该能够找到大量的例子,并能够在你的应用程序中使用它们,而只需要很少的重写。不要再发明轮子了。
if ($_POST["Metod"] == 'Uploadfiles'){

                if ($_POST["Uploadtype"] == 'image')
                {
                    $uploadDirectory = "./uploads/";
                    $pathname = $_POST["Pathname"];
                    $fileName = $_POST["Filename"];
                    $uploadPath = $uploadDirectory . $fileName; 
                    $didUpload = move_uploaded_file($pathname, $uploadPath);
                    if ($didUpload)
                    {
                        echo "Sucsess";
                    }
                    else
                    {
                        echo "Erro In Upload Imgae.";
                    }
                }
            }