Multipart.cs in mutiple parts ))))) just join this and the next post
MultipartForm.cs
----------------
using System;
using System.Net;
using System.Text;
using System.IO;
using System.Collections;
namespace norvanco.http
{
/// <summary>
/// Allow the transfer of data files using the W3C's specification
/// for HTTP multipart form data. Microsoft's version has a bug
/// where it does not format the ending boundary correctly.
/// Written by:
gregoryp@norvanco.com
/// </summary>
public class MultipartForm
{
/// <summary>
/// Holds any form fields and values that you
/// wish to transfer with your data.
/// </summary>
private Hashtable coFormFields;
/// <summary>
/// Used mainly to avoid passing parameters to other routines.
/// Could have been local to sendFile().
/// </summary>
protected HttpWebRequest coRequest;
/// <summary>
/// Used if we are testing and want to output the raw
/// request, minus http headers, out to a file.
/// </summary>
System.IO.Stream coFileStream;
/// <summary>
/// Difined to build the form field data that is being
/// passed along with the request.
/// </summary>
static string CONTENT_DISP = "Content-Disposition: form-data; name=";
/// <summary>
/// Allows you to specify the specific version of HTTP to use for uploads.
/// The dot NET stuff currently does not allow you to remove the continue-100 header
/// from 1.1 and 1.0 currently has a bug in it where it adds the continue-100. MS
/// has sent a patch to remove the continue-100 in HTTP 1.0.
/// </summary>
public Version TransferHttpVersion
{get{return coHttpVersion;}set{coHttpVersion=value;}}
Version coHttpVersion;
/// <summary>
/// Used to change the content type of the file being sent.
/// Currently defaults to: text/xml. Other options are
/// text/plain or binary
/// </summary>
public string FileContentType
{get{return coFileContentType;}set{coFileContentType=value;}}
string coFileContentType;
/// <summary>
/// Initialize our class for use to send data files.
/// </summary>
/// <param name="url">The web address of the recipient of the data transfer.</param>
public MultipartForm(string url)
{
URL = url;
coFormFields = new Hashtable();
ResponseText = new StringBuilder();
BufferSize = 1024 * 10;
BeginBoundary = "ou812--------------8c405ee4e38917c";
TransferHttpVersion = HttpVersion.Version11;
FileContentType = "text/xml";
}
//---------- BEGIN PROPERTIES SECTION ----------
string _BeginBoundary;
/// <summary>
/// The string that defines the begining boundary of
/// our multipart transfer as defined in the w3c specs.
/// This method also sets the Content and Ending
/// boundaries as defined by the w3c specs.
/// </summary>
public string BeginBoundary
{
get{return _BeginBoundary;}
set
{
_BeginBoundary =value;
ContentBoundary = "--" + BeginBoundary;
EndingBoundary = ContentBoundary + "--";
}
}
/// <summary>
/// The string that defines the content boundary of
/// our multipart transfer as defined in the w3c specs.
/// </summary>
protected string ContentBoundary
{get{return _ContentBoundary;}set{_ContentBoundary=value;}}
string _ContentBoundary;
/// <summary>
/// The string that defines the ending boundary of
/// our multipart transfer as defined in the w3c specs.
/// </summary>
protected string EndingBoundary
{get{return _EndingBoundary;}set{_EndingBoundary=value;}}
string _EndingBoundary;
/// <summary>
/// The data returned to us after the transfer is completed.
/// </summary>
public StringBuilder ResponseText
{get{return _ResponseText;}set{_ResponseText=value;}}
StringBuilder _ResponseText;
/// <summary>
/// The web address of the recipient of the transfer.
/// </summary>
public string URL
{get{return _URL;}set{_URL = value;}}
string _URL;
/// <summary>
/// Allows us to determine the size of the buffer used
/// to send a piece of the file at a time out the IO
/// stream. Defaults to 1024 * 10.
/// </summary>
public int BufferSize
{get{return _BufferSize;}set{_BufferSize = value;}}
int _BufferSize;
//---------- END PROPERTIES SECTION ----------
/// <summary>
/// Used to signal we want the output to go to a
/// text file verses being transfered to a URL.
/// </summary>
/// <param name="path"></param>
public void setFilename(string path)
{
coFileStream = new System.IO.FileStream(path,FileMode.Create,FileAccess.Write);
}
/// <summary>
/// Allows you to add some additional field data to be
/// sent along with the transfer. This is usually used
/// for things like userid and password to validate the
/// transfer.
/// </summary>
/// <param name="key">The form field name</param>
/// <param name="str">The form field value</param>
public void setField(string key, string str)
{
coFormFields[key] = str;
}
/// <summary>
/// Determines if we have a file stream set, and returns either
/// the HttpWebRequest stream of the file.
/// </summary>
/// <returns></returns>
public virtual System.IO.Stream getStream()
{
System.IO.Stream io;
if( null == coFileStream )
io = coRequest.GetRequestStream();
else
io = coFileStream;
return io;
}
/// <summary>
/// Here we actually make the request to the web server and
/// retrieve it's response into a text buffer.
/// </summary>
public virtual void getResponse()
{
if( null == coFileStream )
{
System.IO.Stream io;
WebResponse oResponse;
try
{
oResponse = coRequest.GetResponse();
}
catch(WebException web )
{
oResponse = web.Response;
}
if( null != oResponse )
{
io = oResponse.GetResponseStream();
StreamReader sr = new StreamReader(io);
string str;
ResponseText.Length = 0;
while( (str = sr.ReadLine()) != null )
ResponseText.Append(str);
oResponse.Close();
}
else
throw new Exception("MultipartForm: Error retrieving server response");
}
}
/// <summary>
/// Transmits a file to the web server stated in the
/// URL property. You may call this several times and it
/// will use the values previously set for fields and URL.
/// </summary>
/// <param name="aFilename">The full path of file being transfered.</param>