using System; using System.IO; using System.Net; using System.Text; namespace AppLimit.CloudComputing.SharpBox.Common.Net.Web { internal class WebRequestMultipartFormDataSupport { private const string FormBoundary = "-----------------------------28947758029299"; public string GetMultipartFormContentType() { return string.Format("multipart/form-data; boundary={0}", FormBoundary); } public long GetHeaderFooterSize(string fileName) { var header = GetHeaderData(fileName); var footer = GetFooterString(); return header.Length + footer.Length; } /// /// This method prepares a webrequest to be a multpart data requets /// /// public void PrepareWebRequest(WebRequest request) { request.Method = "POST"; request.ContentType = GetMultipartFormContentType(); } /// /// This method opens a network file data stream which means the http(s) /// will be established to the server /// /// /// /// public void PrepareRequestStream(Stream requestStream, string fileName) { // Add just the first part of this param, since we will write the file data directly to the Stream var header = GetHeaderData(fileName); // write the header into stream requestStream.Write(header, 0, header.Length); } /// /// This method writes the footer into the data stream /// /// public void FinalizeNetworkFileDataStream(Stream networkStream) { var encoding = Encoding.UTF8; var footer = GetFooterString(); networkStream.Write(encoding.GetBytes(footer), 0, footer.Length); } /// /// Check if the current request us a multipart upload file /// /// /// public bool IsRequestMultiPartUpload(WebRequest request) { try { if (request.ContentType == null) return false; if (request.ContentType.Equals(GetMultipartFormContentType())) return true; return false; } catch (NotSupportedException) { return false; } } private static byte[] GetHeaderData(string fileName) { var utf8 = new UTF8Encoding(); var data = utf8.GetBytes(string.Format("--{0}{4}Content-Disposition: form-data; name=\"{2}\"; filename=\"{1}\";{4}Content-Type: {3}{4}{4}", FormBoundary, fileName, "file", "application/octet-stream", Environment.NewLine)); return data; } private static string GetFooterString() { return string.Format("{1}--{0}--{1}", FormBoundary, Environment.NewLine); } } }