jeg lavede for et par aar siden denne her kode (den er lidt speciel fordi
den uploader 2 filer, men du kan se lidt af teknikken):
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/upload2.php", "C:\\z.c", "C:\\z.exe");
upload("
http://localhost/upload.php", "C:\\z.c", "C:\\z.exe");
}
public static void upload(string url, string textfile, string binfile)
{
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: attachment; name=\"filename[1]\"; filename=\"" + textfile + "\"");
post.WriteLine("Content-disposition: attachment; name=\"textfile\"; 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.WriteLine("--" + BOUNDARY);
//post.WriteLine("Content-disposition: attachment; name=\"filename[2]\"; filename=\"" + binfile + "\"");
post.WriteLine("Content-disposition: attachment; name=\"binfile\"; filename=\"" + binfile + "\"");
post.WriteLine("Content-type: application/octet-stream");
post.WriteLine("Content-Transfer-Encoding: binary");
post.WriteLine("");
post.Flush();
Stream binf = new FileStream(binfile, FileMode.Open);
int c;
while((c = binf.ReadByte()) >= 0) {
post.BaseStream.WriteByte((byte)c);
}
binf.Close();
post.BaseStream.Flush();
post.Close();
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Console.WriteLine(resp.StatusCode);
resp.Close();
}
}