简体   繁体   English

如何使用POST方法将文件从C#发送到PHP文件(PHP通过“ $ _FILES”功能从HTML表单获取文件)?

[英]How can I send a file with the POST method from C# to a PHP file which (PHP) gets files from a HTML form by the '$_FILES' function?

First 第一

I have a PHP file that gets data and file from a HTML form and uploads a file to a specific directory by the $_POST and $_FILES methods. 我有一个PHP文件,该文件从HTML表单获取数据和文件,并通过$_POST$_FILES方法将文件上传到特定目录。

Second 第二

I have Windows aplication ( C# ). 我有Windows应用程序( C# )。 The user can drag and drop any file to a listview. 用户可以将任何文件拖放到列表视图。 I send the user's data to a PHP file by the POST method and it (the PHP file) upadtes the database. 我通过POST方法将用户数据发送到PHP文件,并且该文件(PHP文件)更新数据库。 My problem is I won't to send file in listview drag and dropped by the user to the PHP file as it could get the file by the $_FILES method and access it. 我的问题是,我不会通过用户拖放到listview中的文件发送到PHP文件,因为它可以通过$_FILES方法获取文件并访问它。

If you want to POST a single file from C# to your PHP script you could use the UploadFile method: 如果要从C#将单个文件发布到PHP脚本,则可以使用UploadFile方法:

using (var client = new WebClient())
{
    client.UploadFile("http://example.com/script.php", "POST", @"C:\path\test.txt");
}

If along with the file you want to send some other data and/or multiple files you will need to forge a multipart/form-data request manually. 如果要与文件一起发送其他数据和/或多个文件,则需要手动创建multipart/form-data请求。 Here's a post explaining how to achieve this. 这是一篇说明如何实现此目标的帖子

了解HTTP协议,并为网站所有者祈祷不要因为违反使用条款而禁止用户的IP地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM