// code from: http://www.heikniemi.net/hc/archives/000205.html // and: http://www.heikniemi.net/hc/archives/files/Upload.cs using System; using System.Collections; using System.Collections.Specialized; using System.IO; using System.Net; namespace eBay_Images { /// /// Provides functionality for programmatically uploading files /// with the HTTP protocol. /// public sealed class HttpUpload { #region Constructor /// /// Private Constructor (prevents the object from being built) /// private HttpUpload() { } #endregion #region UploadFile /// /// Uploads the given file to the given url. /// /// The pathname of the file to be uploaded. /// The URI to which the file shall be sent. /// The name of the form field for the upload. /// Cookies to be sent with the request. /// Login credentials to be passed. public static HttpWebResponse UploadFile(string pathname, Uri uri, string fieldName, CookieContainer cookies, ICredentials credentials) { return Upload(uri, cookies, credentials, new UploadSpec(pathname, fieldName)); } #endregion #region UploadByteArray /// /// Uploads the given byte array to the given url. /// /// The data to be uploaded. /// The name to be sent as the filename. /// The URI to which the file shall be sent. /// The name of the form field for the upload. /// Cookies to be sent with the request. /// Login credentials to be passed. public static HttpWebResponse UploadByteArray(byte[] data, string fileName, Uri uri, string fieldName, CookieContainer cookies, ICredentials credentials) { return Upload(uri, cookies, credentials, new UploadSpec(data, fileName, fieldName)); } #endregion #region Upload /// /// Uploads the given data. /// /// The URI to which the data shall be sent. /// Cookies to be sent with the request. /// Login credentials to be passed. /// The data to be sent. public static HttpWebResponse Upload(Uri uri, CookieContainer cookies, ICredentials credentials, params UploadSpec[] objects) { return Upload(uri, null, cookies, credentials, objects); } /// /// Uploads the given data. /// /// The URI to which the data shall be sent. /// Form fields to be posted along with the file. /// Cookies to be sent with the request. /// Login credentials to be passed. /// The data to be sent. public static HttpWebResponse Upload(Uri uri, StringDictionary formFields, CookieContainer cookies, ICredentials credentials, params UploadSpec[] objects) { // Initialize the request object HttpWebRequest req = (WebRequest.Create(uri) as HttpWebRequest); if (cookies != null) req.CookieContainer = cookies; if (credentials != null) req.Credentials = credentials; string boundary = Guid.NewGuid().ToString("N"); req.ContentType = "multipart/form-data; boundary=" + boundary; req.Method = "POST"; MemoryStream postData = new MemoryStream(); string newLine = "\r\n"; StreamWriter sw = new StreamWriter(postData); if (formFields != null) foreach (DictionaryEntry de in formFields) { sw.Write("--" + boundary + newLine); sw.Write("Content-Disposition: form-data; name=\"{0}\"{1}{1}{2}{1}", de.Key, newLine, de.Value); } foreach (UploadSpec us in objects) { sw.Write("--" + boundary + newLine); sw.Write("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", us.FieldName, us.FileName, newLine); sw.Write("Content-Type: application/octet-stream" + newLine + newLine); sw.Flush(); postData.Write(us.Contents, 0, us.Contents.Length); sw.Write(newLine); } sw.Write("--{0}--{1}", boundary, newLine); sw.Flush(); req.ContentLength = postData.Length; using (Stream s = req.GetRequestStream()) postData.WriteTo(s); postData.Close(); return (req.GetResponse() as HttpWebResponse); } #endregion } /// /// Holds the information about the file(s) to be uploaded. /// public struct UploadSpec { #region Member Variables private byte[] contents; private string fileName; private string fieldName; #endregion #region Properties /// /// The byte array content to be uploaded. /// public byte[] Contents { get { return contents; } set { contents = value; } } /// /// The name of the file to be uploaded. /// public string FileName { get { return fileName; } set { fileName = value; } } /// /// The HTML form field the file should be uploaded into. /// public string FieldName { get { return fieldName; } set { fieldName = value; } } #endregion #region UploadSpec /// /// Creates a new upload spec based on a byte array. /// /// The contents to be uploaded. /// The file to be uploaded. /// The field name as which this file shall be sent to. public UploadSpec(byte[] contents, string fileName, string fieldName) { this.contents = contents; this.fileName = fileName; this.fieldName = fieldName; } /// /// Creates a new upload spec based on a file name. /// /// /// public UploadSpec(string pathname, string fieldName) { using (FileStream inFile = new FileStream(pathname, FileMode.Open)) { byte[] inBytes = new byte[inFile.Length]; inFile.Read(inBytes, 0, inBytes.Length); this.contents = inBytes; } this.fileName = Path.GetFileName(pathname); this.fieldName = fieldName; } #endregion } }