/// <summary> /// Uploads a file. /// </summary> /// <param name="fileInfo"> The UploadFileInfo item.</param> /// <param name="boundaryBytes"> The boundary as bytes.</param> /// <param name="currentStream"> The current stream.</param> protected void UploadFile(UploadFileInfo fileInfo, byte[] boundaryBytes, Stream currentStream) { // Open file FileStream fileStream = new FileStream(fileInfo.FileName, FileMode.Open, FileAccess.Read); // write out file byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))]; int bytesRead = 0; while ( (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0 ) { // Begin Write //currentStream.BeginWrite(data, 0, data.Length, requestState.WriteCallback, requestState); currentStream.Write(buffer, 0, bytesRead); } // Write out the trailing boundary // currentStream.Write(boundaryBytes, 0, boundaryBytes.Length); fileStream.Close(); }
/// <summary> /// Upload the file infos. /// </summary> /// <param name="formTag"> The form tag to get the UploadFileInfo items.</param> /// <returns> An UploadFileInfo array.</returns> public static UploadFileInfo[] GetUploadFiles(HtmlFormTag formTag) { ArrayList list = new ArrayList(); foreach ( HtmlTagBaseList tagBaseList in formTag.AllValues ) { foreach ( HtmlTagBase tag in tagBaseList ) { if ( tag is HtmlInputTag ) { HtmlInputTag input = (HtmlInputTag)tag; if ( input.Type == HtmlInputType.File ) { UploadFileInfo fileInfo = new UploadFileInfo(); // get file name fileInfo.FormFieldName = input.Name; if ( input.Value == null ) { input.Value = string.Empty; } fileInfo.FileName = input.Value.Trim('"').Trim('\0').Trim(); if ( fileInfo.FileName.Length > 0 ) { fileInfo.ContentType = AppLocation.GetMIMEType(fileInfo.FileName); list.Add(fileInfo); } } } } } return (UploadFileInfo[])list.ToArray(typeof(UploadFileInfo)); }
/// <summary> /// Generates a MIME multipart/form-data header. /// </summary> /// <param name="boundary"> The boundary.</param> /// <param name="formTag"> The HtmlFormTag value.</param> /// <param name="fileItems"> The UploadFileInfo items.</param> /// <returns> A MIME multipart/form-data header string.</returns> protected string GenerateMimeFormData(string boundary, Ecyware.GreenBlue.Engine.HtmlDom.HtmlFormTag formTag, UploadFileInfo[] fileItems) { // create hashtable for upload file infos. Hashtable files = new Hashtable(); foreach ( UploadFileInfo fileInfo in fileItems ) { files.Add(fileInfo.FormFieldName, fileInfo); } HtmlParser parser = new HtmlParser(); ArrayList values = parser.GetArrayList(formTag); StringBuilder mimeData = new StringBuilder(); // Create form fields foreach ( string nameValuePair in values ) { string name = EncodeDecode.UrlDecode(nameValuePair.Split('=')[0]); string value = EncodeDecode.UrlDecode(nameValuePair.Split('=')[1].Trim().Trim('\0')).Trim('\0'); // skip keys from UploadFileItems. if ( files.ContainsKey(name) ) { UploadFileInfo fileInfo = (UploadFileInfo)files[name]; //-----------------------------7d52903b1507ec //\r\n //Content-Disposition: form-data; name=\"oFile1\"; filename=\"C:\\images\\GB Splash Screen.psd\" //\r\n //Content-Type: application/octet-stream //\r\n //\r\n mimeData.Append("--"); mimeData.Append(boundary); mimeData.Append("\r\n"); mimeData.AppendFormat("Content-Disposition: form-data; name=\"{0}\"",name); mimeData.AppendFormat("; filename=\"{0}\"",fileInfo.FileName); mimeData.Append("\r\n"); mimeData.Append("Content-Type: "); mimeData.Append(fileInfo.ContentType); mimeData.Append("\r\n"); mimeData.Append("\r\n"); } else { // Example //-----------------------------7d52903b1507ec //\r\n //Content-Disposition: form-data;name="Hello23" //\r\n //\r\n //Hello //\r\n mimeData.Append("--"); mimeData.Append(boundary); mimeData.Append("\r\n"); mimeData.AppendFormat("Content-Disposition: form-data; name=\"{0}\"",name); mimeData.Append("\r\n\r\n"); mimeData.Append(value); mimeData.Append("\r\n"); } } //mimeData.Append("\0"); return mimeData.ToString(); }