制作一个winform自动上传文件和数据到webapiprivate void button1_Click(object sender, EventArgs e)
        {
            
            string url = @"http://192.168.1.100/test.php";//上传地址 
            NameValueCollection dicr = new NameValueCollection();
            dicr.Add("pno", "1"); //项目编号
            dicr.Add("dno", "P01");//设备编号
            dicr.Add("mode", "1");//模式类型 (1,2)
            dicr.Add("datestr", "2019-10-12 10:00:33");//时间字符串
            dicr.Add("filename", "056--161903.jpg");//文件名
            dicr.Add("filepath", "/2019-10-15/");//文件目录
            dicr.Add("carno", "渝AHA105");//车牌号
            dicr.Add("ext", "1");//照片序列
            string sttuas = HttpPostData(url, 100000, "img", "d:\\A4.jpg", dicr);
            MessageBox.Show(sttuas);
        }        //上传文件封装
        private static string HttpPostData(string url, int timeOut, string fileKeyName, string filePath, NameValueCollection stringDict)
        {
            string responseContent;
            var memStream = new MemoryStream();
            var webRequest = (HttpWebRequest)WebRequest.Create(url);
            // 边界符
            var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
            // 边界符
            var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
            var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            // 最后的结束符
            var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
            // 设置属性
            webRequest.Method = "POST";
            webRequest.Timeout = timeOut;
            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            // 写入文件
            const string filePartHeader =
                "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
                 "Content-Type: application/octet-stream\r\n\r\n";
            var header = string.Format(filePartHeader, fileKeyName, filePath);
            var headerbytes = Encoding.UTF8.GetBytes(header);
            memStream.Write(beginBoundary, 0, beginBoundary.Length);
            memStream.Write(headerbytes, 0, headerbytes.Length);
            var buffer = new byte[1024];
            int bytesRead; // =0
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
            {
                memStream.Write(buffer, 0, bytesRead);
            }
            // 写入字符串的Key
            var stringKeyHeader = "\r\n--" + boundary +
                                   "\r\nContent-Disposition: form-data; name=\"{0}\"" +
                                   "\r\n\r\n{1}\r\n";
            foreach (byte[] formitembytes in from string key in stringDict.Keys
                                             select string.Format(stringKeyHeader, key, stringDict[key])
                                                 into formitem
                                                 select Encoding.UTF8.GetBytes(formitem))
            {
                memStream.Write(formitembytes, 0, formitembytes.Length);
            }
            // 写入最后的结束边界符
            memStream.Write(endBoundary, 0, endBoundary.Length);
            webRequest.ContentLength = memStream.Length;
            var requestStream = webRequest.GetRequestStream();
            memStream.Position = 0;
            var tempBuffer = new byte[memStream.Length];
            memStream.Read(tempBuffer, 0, tempBuffer.Length);
            memStream.Close();
            requestStream.Write(tempBuffer, 0, tempBuffer.Length);
            requestStream.Close();
            var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
            using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),Encoding.GetEncoding("utf-8")))
            {
                responseContent = httpStreamReader.ReadToEnd();
            }
            fileStream.Close();
            httpWebResponse.Close();
            webRequest.Abort();
            return responseContent;
        }
接收端的代码为php
<?php
print_r($_POST);
?>
为什么取不到img的文件参数呢,请教?

解决方案 »

  1.   

    不懂php。
    只能助攻你一下了。https://blog.csdn.net/hanjun0612/article/details/60126445
      

  2.   


    这里的 public static string HttpPost(string url, CookieContainer cookieContainer = null, Stream postStream = null, Dictionary<string, string> fileDictionary = null, string refererUrl = null, Encoding encoding = null, int timeOut = 10000)
    使用这个函数上传 d:\\a4.jpg 和 参数  pno = 1  , ext = 2 该如何应用?
      

  3.   

    CookieContainer 传null
    参数都放到Dictionary<string, string> fileDictionary里面
      

  4.   

    你的边界确认没问题?建议分两部分传递:参数+图片,装在一个类中
    参数直接序列化json,图片传byte
    php咱不懂,要是它只接受byte数组,那你你在php端再加个方法,将json转对象再转byte