最近在作這個,一開始也是不明白爲何給個URL帶着兩個參數就直接上傳了,網上看了不少都是PHP,可是PHP沒看過是不會 的html
因此就一直在找網上什麼Demo之類的講解,最後仍是不錯找到了一個比較好理解的例子。json
整個過程是這樣的:api
一、咱們首先建一個項目服務器
二、項目建好後就一個簡單的file控件和一個submit提交按鈕咱們這裏要用表單提交微信
上代碼:cookie
<html>
<head>
<title>qweqw</title>
</head>
<body>
@using (Html.BeginForm("Upload", "Source", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<text>選擇上傳文件:</text><input name="file" type="file" id="file" />
<br />
<br />
<input type="submit" name="Upload" value="Upload" />
}
</body>
</html>app
三、前臺完了那就到後臺了對吧post
看代碼:ui
[HttpPost]
public ActionResult Upload(FormCollection form)
{
if (Request.Files.Count == 0)
{
//Request.Files.Count 文件數爲0上傳不成功
return View();
}
var file = Request.Files[0];
if (file.ContentLength == 0)
{
//文件大小大(以字節爲單位)爲0時,作一些操做
return View();
}
else
{
//文件大小不爲0
HttpPostedFileBase files = Request.Files[0];
//保存成本身的文件全路徑,newfile就是你上傳後保存的文件,
string newFile = DateTime.Now.ToString("yyyyMMddHHmmss") ;
Access_token model = new Access_token();
model = pub.Check_Token();
string type = "image";
string url = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", model.access_token, type.ToString());
string path = "D://項目//我的//Senparc.Weixin.MP.Sample//Senparc.Weixin.MP.Sample//image//" + files.FileName;
//服務器上的UpLoadFile文件夾必須有讀寫權限
files.SaveAs(path);
string filename = System.Web.HttpContext.Current.Server.MapPath("/image/" + files.FileName);
string json = HttpUploadFile(url, filename);
JObject jb = (JObject)JsonConvert.DeserializeObject(json);//這裏就能知道返回正確的消息了下面是我的的邏輯我就沒寫url
..........................................
}
return Content("成功");
}
public static string HttpUploadFile(string url, string path)//這個方法是兩個URL第一個url是條到微信的,第二個是本地圖片路徑
{
// 設置參數
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
string boundary = DateTime.Now.Ticks.ToString("X"); // 隨機分隔線
request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
int pos = path.LastIndexOf("\\");
string fileName = path.Substring(pos + 1);
//請求頭部信息
StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName));
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] bArr = new byte[fs.Length];
fs.Read(bArr, 0, bArr.Length);
fs.Close();
Stream postStream = request.GetRequestStream();
postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
postStream.Write(bArr, 0, bArr.Length);
postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
postStream.Close();
//發送請求並獲取相應迴應數據
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序纔開始向目標網頁發送Post請求
Stream instream = response.GetResponseStream();
StreamReader sr = new StreamReader(instream, Encoding.UTF8);
//返回結果網頁(html)代碼
string content = sr.ReadToEnd();
return content;
}
四、前臺的這句話@using (Html.BeginForm("Upload", "Source", FormMethod.Post, new { enctype = "multipart/form-data" }))中
new { enctype = "multipart/form-data" }是必需要的,
這樣就會訪問到Upload這個方法接下來就兩個方法的執行了那麼就看第3步驟了