国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#批量上傳圖片到服務器

2019-11-15 02:32:24
字體:
來源:轉載
供稿:網友

C#批量上傳圖片到服務器

客戶端代碼:

 /// <summary>        /// 批量上傳圖片        /// </summary>        /// <param name="srcurl">服務器路徑</param>        /// <param name="imagesPath">圖片文件夾路徑</param>        /// <param name="files">圖片名稱</param>        public void UpLoadFile(string srcurl, string imagesPath, List<string> files)        {            int count = 1;            foreach (string imageName in files)            {                string name = imageName;                string url = null;                //+  加號特殊處理                if (name.Contains("+"))                {                    url = srcurl + "name=" + name.Replace("+", "%2B");                }                else                {                    url = srcurl + "name=" + name;                }                FileStream fs = new FileStream(imagesPath + name, FileMode.Open);                byte[] data = new byte[fs.Length];                fs.Read(data, 0, data.Length);                fs.Close();                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);                request.ContentType = "image/jpeg";                request.Method = "POST";                Encoding encoding = Encoding.UTF8;                request.ContentLength = data.Length;                Stream requestStream = request.GetRequestStream();                requestStream.Write(data, 0, data.Length);                requestStream.Close();                HttpWebResponse response = (HttpWebResponse)request.GetResponse();                StreamReader streamReader = new StreamReader(response.GetResponseStream(), encoding);                string retString = streamReader.ReadToEnd();                streamReader.Close();                Console.WriteLine((count++) + "/" + files.Count);            }        }

服務器端代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Net;using System.Text;using System.IO;public partial class upload : System.Web.UI.Page{        PRotected void Page_Load(object sender, EventArgs e)    {        string fPath = Server.MapPath("服務器端圖片存儲的虛擬目錄名稱");//得到虛擬目錄的真實路徑//檢查存儲目錄        if (!Directory.Exists(fPath))        {            Directory.CreateDirectory(fPath);        }        string name = Request.QueryString["name"];//得到文件名        HttpUtility.UrlEncode(name, Encoding.GetEncoding("UTF-8"));                if (name != null)        {            if (!File.Exists(fPath + name))            {                System.IO.Stream stream = Request.InputStream;                byte[] buffer = new byte[stream.Length];                FileStream fs = null;                try                {                    fs = new FileStream(fPath + name, FileMode.Create);                    while ((stream.Read(buffer, 0, buffer.Length)) > 0)                    {                        fs.Write(buffer, 0, buffer.Length);                    }                }                catch (IOException ioe)                {                    Response.Write(ioe);                }                finally                {                    if (fs != null)                    {                        fs.Close();                    }                    stream.Close();                }                Response.Write(name + "<br>");                Response.Write(File.Exists(fPath + name) + "<br>");            }        }        Response.Write("上傳完畢" + Directory.Exists(fPath) + Path.GetFullPath(fPath));    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永兴县| 商南县| 清丰县| 临洮县| 安福县| 息烽县| 桦南县| 林芝县| 邳州市| 田阳县| 新竹县| 伊金霍洛旗| 临沭县| 阜南县| 息烽县| 连城县| 福鼎市| 定边县| 灵台县| 朝阳区| 盐源县| 丽水市| 伊金霍洛旗| 林周县| 农安县| 汤阴县| 元阳县| 建平县| 沾益县| 余庆县| 梁平县| 错那县| 庐江县| 通州区| 贡觉县| 霞浦县| 长寿区| 铁岭市| 宜君县| 阿勒泰市| 神农架林区|