Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Webclient类上载文件_C#_Php_File Upload - Fatal编程技术网

C# 使用Webclient类上载文件

C# 使用Webclient类上载文件,c#,php,file-upload,C#,Php,File Upload,这是表单的代码: form action="upload.php" method="post" enctype="multipart/form-data" input type="file" name="myFile" br input type="submit" value="Upload" /form 这是服务器端php脚本: if (!empty($_FILES["myFile"])) { $myFile = $_FILES["myFile"]; if ($m

这是表单的代码:

   form action="upload.php" method="post" enctype="multipart/form-data" 
   input type="file" name="myFile" br
   input type="submit" value="Upload"
   /form
这是服务器端php脚本:
if (!empty($_FILES["myFile"])) {
$myFile = $_FILES["myFile"];

if ($myFile["error"] !== UPLOAD_ERR_OK) {
    echo "<p>An error occurred.</p>";
    exit;
}

// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
    $i++;
    $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}

// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
    UPLOAD_DIR . $name);
if (!$success) { 
    echo "<p>Unable to save file.</p>";
    exit;
}

// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);
}

为了让它显示出来,我不得不修改html。所以问题是webclient在我尝试上传文件时没有显示异常,我已经打印了返回值,没有任何内容,而且似乎工作正常,只是文件没有添加到目录中。如果我自己使用表单按钮,单击browse等,那么它就可以正常工作。权限应该很好,这一切都是手工完成的,我不明白我怎么会弄乱webclient代码,它只有一行长,我已经检查了无数次url,路径是100%正确的。

在我看来,你好像在使用HTML表单来测试你的服务器端PHP是否正常工作

看看你的html
。发布时,数据保存在
$\u FILES
数组中,名称
myFile
作为键


当您通过C#的
WebClient
发布时,它会选择名称。尝试修改你的服务器端PHP来检查<代码> $x文件[文件] < /代码>,它应该工作。< /P>如果下面的答案是正确的,请考虑接受它。因此,当WebCalver张贴时,它使用的是“文件”,而我的服务器端PHP使用的是“Myfile”。有没有办法指定webclient对文件调用的内容,还是总是“文件”?thanks@hydrozoah我以前没有以这种方式使用过WebClient,但请检查一下。使用此选项可能有助于:
private static WebClient _client = new WebClient(); 
private static Uri _address = new Uri("http://tildetictac.x10host.com/upload.php");

_logPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\log.txt"; //path to the log txt file  
try
    {
        byte[] ret = _client.UploadFile(_address, "POST", _logPath);
    }
    catch (WebException e)
    {
        Console.WriteLine(e.StackTrace);
    }