POST 方式上传图片_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > POST 方式上传图片

POST 方式上传图片

 2014/12/10 11:00:14  juk  程序员俱乐部  我要评论(0)
  • 摘要:Post方式模仿form表单上传图片设置enctype=multipart/form-data<formenctype="multipart/form-data"></form> 自定义MultipartFormData类 (4.5以上版本好像有这个类 类名称叫啥忘记了)///<summary>///multipart/form-data数据类型///</summary>
  • 标签:图片 上传 方式

Post 方式 模仿 form表单 上传 图片 设置enctype = multipart/form-data

class="brush: csharp; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><form  enctype="multipart/form-data"></form>

 

自定义 MultipartFormData 类  (4.5以上版本好像有这个类  类名称叫啥忘记了)

/// <summary>
    /// multipart/form-data 数据类型
    /// </summary>
    public class MultipartFormData
    {
        public static string ContentType = "multipart/form-data; boundary=A300x";
        private string headerStream = "--A300x\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";
        private string footerString = "--A300x--\r\n";
        private string compart = "\r\n";
        private string headerString = "--A300x\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n";

        private List<byte> lists = new List<byte>();
        private byte[] footerBytes;
        private byte[] comparts;
        public MultipartFormData()
        {
            footerBytes = Encoding.UTF8.GetBytes(footerString);
            comparts = Encoding.UTF8.GetBytes(compart);           
        }
        public byte[] Buffer
        {
            get
            {
                return lists.ToArray();
            }
        }


        private bool IsEnd = false;
        /// <summary>
        ///
        /// </summary>
        public void End()
        {
            if (!IsEnd)
            {
                IsEnd = true;
                //lists.AddRange(footerBytes.ToList());   
                this.AddRanges(footerBytes);
            }
         
        }

        public void AddRanges(byte[] array) {
            foreach (byte b in array)
                lists.Add(b);
        }

        /// <summary>
        /// 向序列化到多部/窗体数据 MIME 类型的 对象集合添加 HTTP 内容
        /// </summary>
        /// <param name="content">要添加到集合中的 HTTP 内容</param>
        /// <param name="name">要添加的 HTTP 内容的名称</param>
        public void Add(string content, string name)
        {

            byte[] headerBytes = Encoding.UTF8.GetBytes(String.Format(headerString, name));
            this.AddRanges(headerBytes);
            byte[] buffer = Encoding.UTF8.GetBytes(content);
            this.AddRanges(buffer);
            this.AddRanges(comparts);

        }

        /// <summary>
        /// 向序列化到多部/窗体数据 MIME 类型的 对象集合添加 HTTP 内容
        /// </summary>
        /// <param name="content">要添加到集合中的 HTTP 内容</param>
        /// <param name="name">要添加的 HTTP 内容的名称</param>
        /// <param name="fileName">要添加到集合中的 HTTP 内容的文件名</param>
        public void Add(Stream content, string name, string fileName)
        {
            byte[] headerBytes = Encoding.UTF8.GetBytes(String.Format(headerStream, name, fileName));
            this.AddRanges(headerBytes);
            byte[] buffer = new byte[content.Length];
            content.Read(buffer, 0, buffer.Length);
            content.Seek(0, SeekOrigin.Begin);
            this.AddRanges(buffer);
            this.AddRanges(comparts);
        }

        /// <summary>
        /// 向序列化到多部/窗体数据 MIME 类型的 对象集合添加 HTTP 内容
        /// </summary>
        /// <param name="content">要添加到集合中的 HTTP 内容</param>
        /// <param name="name">要添加的 HTTP 内容的名称</param>
        /// <param name="fileName">要添加到集合中的 HTTP 内容的文件名</param>
        public void Add(byte[] content, string name, string fileName)
        {
            byte[] headerBytes = Encoding.UTF8.GetBytes(String.Format(headerStream, name, fileName));
            this.AddRanges(headerBytes);
            this.AddRanges(content);
            this.AddRanges(comparts);
        }

    }
 
 
上传图片代码
        /// <summary>
        /// form-data 数据类型传递数据
        /// </summary>
        private MultipartFormData multiparFormData = new MultipartFormData();

        /// <summary>
        /// 键值对应
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AddFormData(string key, string value)
        {
            this.multiparFormData.Add(value, key);
        }
		/// <summary>
        /// 传递一个 strem 类型
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="fileName"></param>
        public void AddFormData(string key, Stream value, string fileName)
        {
            this.multiparFormData.Add(value, key, fileName);
        }
        /// <summary>
        /// 传递一个 byte[] 类型
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="fileName"></param>
        public void AddFormData(string key, byte[] value, string fileName)
        {
            this.multiparFormData.Add(value, key, fileName);
        }
        private static CookieContainer m_Cookie = new CookieContainer();
		/// <summary>
        /// POST form-data 方式请求消息
        /// </summary>
        /// <param name="url"></param>
        /// <param name="isHttps"></param>
        /// <returns></returns>
        public string PostFromData(string url, bool isHttps) {
            try
            {
                multiparFormData.End();

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = MultipartFormData.ContentType;
                request.CookieContainer = m_Cookie;//设置上一个访问页面的cookie 保持session
                request.ContentLength = multiparFormData.Buffer.Length;

                Stream stream = request.GetRequestStream();

                stream.Write(multiparFormData.Buffer, 0, multiparFormData.Buffer.Length);
                stream.Close();

                WebResponse response = request.GetResponse();
                Stream streamResponse = response.GetResponseStream();
                StreamReader sr = new StreamReader(streamResponse);

                return sr.ReadToEnd();
            }
            catch (Exception ex) {
                Log.WriteLog("POST form-data 方式请求消息:" + ex.Message);
                return "error";
            }
        }
发表评论
用户名: 匿名