制作一個winform自動上傳檔案和資料到webapi
private 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的檔案引數呢,請教?
uj5u.com熱心網友回復:
不懂php。只能助攻你一下了。https://blog.csdn.net/hanjun0612/article/details/60126445
uj5u.com熱心網友回復:
這里的 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 該如何應用?
uj5u.com熱心網友回復:
CookieContainer 傳null引數都放到Dictionary<string, string> fileDictionary里面
uj5u.com熱心網友回復:
你的邊界確認沒問題?建議分兩部分傳遞:引數+圖片,裝在一個類中
引數直接序列化json,圖片傳byte
php咱不懂,要是它只接受byte陣列,那你你在php端再加個方法,將json轉物件再轉byte
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/102013.html
標籤:C#
