从C锋利客户端应用程序上传到PHP服务器 [英] Upload to PHP server from c sharp client application

查看:157
本文介绍了从C锋利客户端应用程序上传到PHP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个C锋利的应用程序(客户端应用程序)。和Web应用程序编写PHP。我想,只要在​​客户端执行特定操作来传输一些文件。下面是客户端代码把文件上传到PHP服务器..

 私人无效的button1_Click(对象发件人,EventArgs五)
{
System.Net.WebClient客户端=新System.Net.WebClient();

Client.Headers.Add(内容类型,二进制/八位字节流);

字节[]结果= Client.UploadFile(HTTP://localhost/project1/upload.php,POST,
@C:\test\a。 JPG);

字符串s = System.Text.Encoding.UTF8.GetString(结果为0,result.Length);
}

下面是upload.php的文件,将文件移动..

  $ uploads_dir ='./files/'; //目录保存来自客户端应用程序的文件。 
的foreach($ _FILES [图片] [错误]为$关键=> $错误){
如果($错误== UPLOAD_ERR_OK){
$ tmp_name的值= $ _FILES [图片] [tmp_name的值] [$关键];
$ NAME = $ _FILES [图片] [名称] [$关键];
move_uploaded_file($ tmp_name的值,$ uploads_dir / $名称);
}



我没有从上面的代码中得到任何错误。但它似乎并不奏效。为什么?我这么想吗?


解决方案

您当前的PHP代码是处理的多个文件上传,但你的C#代码仅上传有一个文件。



您需要稍微修改你的PHP代码,删除的foreach 循环:



<预类=郎PHP prettyprint-覆盖> < PHP
$ uploads_dir ='./files'; //目录保存来自客户端应用程序的文件。
如果($ _FILES [文件] [错误] == UPLOAD_ERR_OK){
$ tmp_name的值= $ _FILES [文件] [tmp_name的值];
$ NAME = $ _FILES [文件] [名称];
move_uploaded_file($ tmp_name的值,$ uploads_dir / $名称);
}
>?;

您还需要确保 ./文件目录存在。



我已经测试过上面的PHP代码与C#代码它完美地工作。




Currently i have a c sharp application (Client app). and a web application written php. I want to transfer some files whenever a particular action is performed at client side. Here is the client code to upload the file to php server..

private void button1_Click(object sender, EventArgs e)
{
    System.Net.WebClient Client = new System.Net.WebClient();

    Client.Headers.Add("Content-Type", "binary/octet-stream");

    byte[] result = Client.UploadFile("http://localhost/project1/upload.php", "POST",
                                      @"C:\test\a.jpg");

    string s = System.Text.Encoding.UTF8.GetString(result, 0, result.Length); 
}

Here is the upload.php file to move the file..

$uploads_dir = './files/'; //Directory to save the file that comes from client application.
foreach ($_FILES["pictures"]["error"] as $key => $error) {
  if ($error == UPLOAD_ERR_OK) {
     $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
     $name = $_FILES["pictures"]["name"][$key];
     move_uploaded_file($tmp_name, "$uploads_dir/$name");
 }

I'm not getting any errors from above code. but it does not seem to be working. Why is it? Am i missing something?

解决方案

Your current PHP code is for handling multiple file uploads, but your C# code is only uploading one file.

You need to modify your PHP code somewhat, removing the foreach loop:

<?php
$uploads_dir = './files'; //Directory to save the file that comes from client application.
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["file"]["tmp_name"];
    $name = $_FILES["file"]["name"];
    move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
?>

You also need to ensure that the ./files directory exists.

I have tested the above PHP code with your C# code and it worked perfectly.

这篇关于从C锋利客户端应用程序上传到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆