WP如何上传文件给PHP
PHP接收文件时需要用 $_FILES["file"]["name"];, HttpWebRequest在上传文件时怎么给文件加个"name"的字段名呢
[解决办法]
HttpWebRequest upLoadFileRequest = HttpWebRequest.CreateHttp("http://phpurl");
upLoadFileRequest.Method = "POST";
upLoadFileRequest.ContentType = "multipart/form-data";
upLoadFileRequest.BeginGetRequestStream((ar) =>
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
using (StreamWriter bodyStream = new StreamWriter(request.EndGetRequestStream(ar)))
{
byte[] fileStreams = null;//把你要传的文件读成字节流
bodyStream.WriteLine(String.Format("Content-Disposition: form-data; name="{0}"; filename="{1}"", "name", "123.css"));
bodyStream.WriteLine(String.Format("Content-Type: {0}", "text/css"));
bodyStream.BaseStream.Write(fileStreams, 0, fileStreams.Length);
request.BeginGetResponse((ar2) =>
{
//发送请求完成
}, request);
}
}, upLoadFileRequest);