Upload to PHP server from c sharp client application

后端 未结 2 1498
悲哀的现实
悲哀的现实 2020-12-15 07:17

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 sid

相关标签:
2条回答
  • 2020-12-15 07:48

    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.

    • For more information on handling file uploads, refer to the PHP documentation.

    • For more information on uploading multiple files with C# and PHP, here are some helpful links:

      Upload files with HTTPWebrequest (multipart/form-data)

      Use Arrays in HTML Form Variables

      PHP: Uploading multiple files

      If you want something simple for uploading multiple files, you just just upload one file at a time to upload.php in a C# loop.

    0 讨论(0)
  • 2020-12-15 07:54

    Your php code seems right, however you try to access the file using the "picture" key of the $_FILES global. It does not seems to be specified in your csharp code. I don't know how to do it thought. You could try to see how it was named in your php by doing a print_r or vardump of you $_FILE global or using the array_keys function

    Regards

    Edit: I found this link that could help you to add a "name" to your uploaded file: http://www.bratched.com/en/home/dotnet/69-uploading-multiple-files-with-c.html

    0 讨论(0)
提交回复
热议问题