C# WPF ASP.net 上傳多檔案和資料

來源:互聯網
上載者:User

標籤:

 

C# WinForm 上傳多檔案和資料

 

public static class HttpHelper    {        private static readonly Encoding DEFAULTENCODE = Encoding.UTF8;        /// <summary>        /// HttpUploadFile        /// </summary>        /// <param name="url"></param>        /// <param name="file"></param>        /// <param name="data"></param>        /// <returns></returns>        public static string HttpUploadFile(string url, string file, NameValueCollection data)        {            return HttpUploadFile(url, file, data, DEFAULTENCODE);        }        /// <summary>        /// HttpUploadFile        /// </summary>        /// <param name="url"></param>        /// <param name="file"></param>        /// <param name="data"></param>        /// <param name="encoding"></param>        /// <returns></returns>        public static string HttpUploadFile(string url, string file, NameValueCollection data, Encoding encoding)        {            return HttpUploadFile(url, new string[] { file }, data, encoding);        }        /// <summary>        /// HttpUploadFile        /// </summary>        /// <param name="url"></param>        /// <param name="files"></param>        /// <param name="data"></param>        /// <returns></returns>        public static string HttpUploadFile(string url, string[] files, NameValueCollection data)        {            return HttpUploadFile(url, files, data, DEFAULTENCODE);        }        /// <summary>        /// HttpUploadFile        /// </summary>        /// <param name="url"></param>        /// <param name="files"></param>        /// <param name="data"></param>        /// <param name="encoding"></param>        /// <returns></returns>        public static string HttpUploadFile(string url, string[] files, NameValueCollection data, Encoding encoding)        {            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");            byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");            byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");            //1.HttpWebRequest            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);            request.ContentType = "multipart/form-data; boundary=" + boundary;            request.Method = "POST";            request.KeepAlive = true;            request.Credentials = CredentialCache.DefaultCredentials;            using (Stream stream = request.GetRequestStream())            {                //1.1 key/value                string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";                if (data != null)                {                    foreach (string key in data.Keys)                    {                        stream.Write(boundarybytes, 0, boundarybytes.Length);                        string formitem = string.Format(formdataTemplate, key, data[key]);                        byte[] formitembytes = encoding.GetBytes(formitem);                        stream.Write(formitembytes, 0, formitembytes.Length);                    }                }                //1.2 file                string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n";                byte[] buffer = new byte[4096];                int bytesRead = 0;                for (int i = 0; i < files.Length; i++)                {                    stream.Write(boundarybytes, 0, boundarybytes.Length);                    string header = string.Format(headerTemplate, "file" + i, Path.GetFileName(files[i]));                    byte[] headerbytes = encoding.GetBytes(header);                    stream.Write(headerbytes, 0, headerbytes.Length);                    using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))                    {                        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)                        {                            stream.Write(buffer, 0, bytesRead);                        }                    }                }                //1.3 form end                stream.Write(endbytes, 0, endbytes.Length);            }            //2.WebResponse            HttpWebResponse response = (HttpWebResponse)request.GetResponse();            using (StreamReader stream = new StreamReader(response.GetResponseStream()))            {                return stream.ReadToEnd();            }        }    }

 

檔案過大解決:

 

修改伺服器設定檔web.config:

添加

    <httpRuntime executionTimeout="10" maxRequestLength="51200"  useFullyQualifiedRedirectUrl="true" minFreeThreads="8"  minLocalRequestFreeThreads="4" appRequestQueueLimit="100"  enableVersionHeader="true"/>

 

C# WPF ASP.net 上傳多檔案和資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.