通过C#将文件发送到我的PHP脚本

时间:2013-02-05 20:17:39

标签: c# php file file-upload

我最近一直在研究我的第一个C#应用程序而且我已经完成了界面工作 - 它非常完美。

现在我陷入了打开文件对话框的部分,然后将该文件发送到位于 localhost (wamp)上的PHP脚本。

这就是我一直在尝试做的事情:

        private void menu_upload_file_Click(object sender, EventArgs e)
        {
            DialogResult dialogOpened = openFileDialog1.ShowDialog();
            if (dialogOpened == DialogResult.OK)
            {
                string filename = openFileDialog1.FileName;

                using (var client = new WebClient())
                {
                    client.UploadFile("http://dugi/imgitv3/upload.php?submit=true&action=upload", "POST", filename);
                }
            }
        }

什么都没发生,文件没有被发送(复制)到我想要的位置。

您可能会理解我在打开打开文件对话框方面取得了成功,但我不知道我是否正在捕获文件名/目录并将其发送到我的网站以便它可以处理它

也许网址很复杂?我需要发送那些提交和操作参数或脚本不会加载。

OH AND:在PHP中,$_FILES数组必须命名为images$_FILES['images']),也许这就是问题? (想到这里问这个)。

那么该文件没有发送到我的网站有什么问题?

2 个答案:

答案 0 :(得分:2)

假设client.UploadFile("http://dugi/imgitv3/upload.php?submit=true&action=upload", "POST", filename);没有抛出异常,请尝试查看响应。

var response = client.UploadFile("http://dugi/imgitv3/upload.php?submit=true&action=upload",  "POST", filename);

Console.WriteLine("\nResponse Received.The contents of the file uploaded are:\n{0}",
                System.Text.Encoding.ASCII.GetString(response));

答案 1 :(得分:1)

问题出在我的PHP脚本中。我总是在那里寻找$_FILES['images']而没有别的。然后在一些谷歌之后,我注意到C#发送了以file$_FILES['file'])命名的文件数组。

我在PHP脚本中进行了必要的更改,现在一切正常。