带有 WebClient POST 值的 UploadFile [英] UploadFile with POST values by WebClient

查看:15
本文介绍了带有 WebClient POST 值的 UploadFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 WebClient 类将文件上传到主机.我还想传递一些应该显示在服务器部分 (PHP) 的 $_POST 数组中的值.我想一键搞定

I want to upload file to a host by using WebClient class. I also want to pass some values which should be displayed in the $_POST array on the server part (PHP). I want to do it by one connect

我使用了下面的代码

using (WebClient wc = new WebClient())
{
    wc.Encoding = Encoding.UTF8;
    NameValueCollection values = new NameValueCollection();
    values.Add("client", "VIP");
    values.Add("name", "John Doe"); 
    wc.QueryString = values; // this displayes in $_GET
    byte[] ans= wc.UploadFile(address, dumpPath);
}

如果我使用了 QueryString 属性,则 $_GET 数组中显示的值.但我想通过 post 方法发送

If i've used QueryString property, the values displayed in $_GET array.But i want to send it by post method

推荐答案

没有任何内置功能可以让您做到这一点.我有博客关于您可以使用的扩展.以下是相关类:

There's nothing built-in that allows you to do that. I have blogged about an extension that you could use. Here are the relevant classes:

public class UploadFile
{
    public UploadFile()
    {
        ContentType = "application/octet-stream";
    }
    public string Name { get; set; }
    public string Filename { get; set; }
    public string ContentType { get; set; }
    public Stream Stream { get; set; }
}

public byte[] UploadFiles(string address, IEnumerable<UploadFile> files, NameValueCollection values)
{
    var request = WebRequest.Create(address);
    request.Method = "POST";
    var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
    request.ContentType = "multipart/form-data; boundary=" + boundary;
    boundary = "--" + boundary;

    using (var requestStream = request.GetRequestStream())
    {
        // Write the values
        foreach (string name in values.Keys)
        {
            var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
            requestStream.Write(buffer, 0, buffer.Length);
            buffer = Encoding.ASCII.GetBytes(string.Format("Content-Disposition: form-data; name="{0}"{1}{1}", name, Environment.NewLine));
            requestStream.Write(buffer, 0, buffer.Length);
            buffer = Encoding.UTF8.GetBytes(values[name] + Environment.NewLine);
            requestStream.Write(buffer, 0, buffer.Length);
        }

        // Write the files
        foreach (var file in files)
        {
            var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
            requestStream.Write(buffer, 0, buffer.Length);
            buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name="{0}"; filename="{1}"{2}", file.Name, file.Filename, Environment.NewLine));
            requestStream.Write(buffer, 0, buffer.Length);
            buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
            requestStream.Write(buffer, 0, buffer.Length);
            file.Stream.CopyTo(requestStream);
            buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
            requestStream.Write(buffer, 0, buffer.Length);
        }

        var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
        requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
    }

    using (var response = request.GetResponse())
    using (var responseStream = response.GetResponseStream())
    using (var stream = new MemoryStream())
    {
        responseStream.CopyTo(stream);
        return stream.ToArray();
    }
}

现在您可以在您的应用程序中使用它:

and now you could use it in your application:

using (var stream = File.Open(dumpPath, FileMode.Open))
{
    var files = new[] 
    {
        new UploadFile
        {
            Name = "file",
            Filename = Path.GetFileName(dumpPath),
            ContentType = "text/plain",
            Stream = stream
        }
    };

    var values = new NameValueCollection
    {
        { "client", "VIP" },
        { "name", "John Doe" },
    };

    byte[] result = UploadFiles(address, files, values);
}

现在在你的 PHP 脚本中你可以使用 $_POST["client"]$_POST["name"]$_FILES["file"].

Now in your PHP script you could use the $_POST["client"], $_POST["name"] and $_FILES["file"].

这篇关于带有 WebClient POST 值的 UploadFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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