上传图片到第三方服务器
Posted 邢帅杰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上传图片到第三方服务器相关的知识,希望对你有一定的参考价值。
代码
/// <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; }
以上是关于上传图片到第三方服务器的主要内容,如果未能解决你的问题,请参考以下文章