上傳圖片到第三方伺服器
阿新 • • 發佈:2018-10-31
程式碼
/// <summary> /// 上傳圖片到第三方伺服器 /// </summary> /// <param name="filePath"></param> /// <param name="picNo"></param> /// <returns></returns> public string UploadFilesToBlueBox(string filePath, string picNo) { string strReturn = "";string fileName = Path.GetFileName(filePath); string strPostUrl = "http://pic.xxx.com/HttpPost_Upload.aspx?PICID=" + picNo + "&FILENAME=" + fileName; HttpWebRequest reqPost = (HttpWebRequest)WebRequest.Create(strPostUrl); reqPost.Method = "POST"; reqPost.KeepAlive = false; reqPost.ContentType = "application/x-www-form-urlencoded"; byte[] fileStream = AuthGetFileData(filePath);//把檔案轉為bute[],看之前的部落格 string strStream = Convert.ToBase64String(fileStream); byte[] strFiledata = Encoding.UTF8.GetBytes(strStream); reqPost.ContentLength= strFiledata.Length; //post資料 using (Stream newStream = reqPost.GetRequestStream()) { newStream.Write(strFiledata, 0, strFiledata.Length); //獲取返回 HttpWebResponse myResponse = (HttpWebResponse)reqPost.GetResponse(); using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8)) { //返回結果 strReturn = reader.ReadToEnd(); } } return strReturn; }