WebClient上传带有POST值的UploadFile

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

我用过下面的代码

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方法发送它

回答:

没有内置功能可让您执行此操作。我已经在博客中发布了可以使用的扩展程序。以下是相关的类:

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();

}

}

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

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"]

以上是 WebClient上传带有POST值的UploadFile 的全部内容, 来源链接: utcz.com/qa/426319.html

回到顶部