// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Net; public class FileUpload { private const string BOUNDARY = "ArneArne"; public static void Main(string[] args) { upload("http://localhost/upload.php", "C:\\z.c"); } public static void upload(string url, string textfile) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "multipart/form-data, boundary=" + BOUNDARY; StreamWriter post = new StreamWriter(req.GetRequestStream()); post.WriteLine("--" + BOUNDARY); post.WriteLine("Content-disposition: form-data; name=\"f\"; filename=\"" + textfile + "\""); post.WriteLine("Content-type: text/plain"); post.WriteLine(""); StreamReader txtf = new StreamReader(new FileStream(textfile, FileMode.Open)); string line; while ((line = txtf.ReadLine()) != null) { post.WriteLine(line); } txtf.Close(); post.BaseStream.Flush(); post.WriteLine("--" + BOUNDARY + "--"); post.Close(); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Console.WriteLine(resp.StatusCode); resp.Close(); } }