Monday, August 16, 2004

File Uploading

Well, this has to be, by far, the ugliest piece of code I've ever written, but... it works. :)  I've been struggling with file uploading for a while, but now it's done. Here's the function that will upload an image to ImageArk and return result HTML. Have fun.

static string FileUpload(string url, string referer, string filename)

{

Random r = new Random();

string randStr = r.Next().ToString();

string dataBoundary = "--xyz"+ randStr;

HttpWebRequest Req =(HttpWebRequest)WebRequest.Create(url);

Req.UserAgent = "Upload Test";

Req.ContentType = "multipart/form-data; boundary=xyz"+randStr;

Req.Method = "POST";

Req.KeepAlive = true;

byte[] FileData = File.ReadAllBytes(filename);

StringBuilder DataString = new StringBuilder();

StringBuilder DataString2 = new StringBuilder();

DataString.Append(dataBoundary + "\r\n");

DataString.Append("Content-Disposition: form-data; name=\"userfile\"; filename=\"File1.jpg\"\r\n");

DataString.Append("Content-Length: " + FileData.Length.ToString() + "\r\n");

DataString.Append("Content-Type: image/jpeg\r\n\r\n");

byte[] tmpPostdata1 = System.Text.Encoding.Default.GetBytes(DataString.ToString());

DataString2.Append(dataBoundary + "\r\n\r\n");

DataString2.Append("Content-Disposition: form-data; name=\"MAX_FILE_SIZE\"\r\n\r\n512000\r\n\r\n");

DataString2.Append(dataBoundary + "--\r\n");

byte[] tmpPostdata2 = System.Text.Encoding.Default.GetBytes(DataString2.ToString());

byte[] t = new byte[tmpPostdata1.Length + FileData.Length + tmpPostdata2.Length];

tmpPostdata1.CopyTo(t, 0);

FileData.CopyTo(t,tmpPostdata1.Length);

tmpPostdata2.CopyTo(t, tmpPostdata1.Length + FileData.Length);

Req.ContentLength = t.Length;

Req.Referer = referer;

Stream tempStream = Req.GetRequestStream();

tempStream.Write(t,0,t.Length);

tempStream.Close();

HttpWebResponse Resp = (HttpWebResponse)Req.GetResponse();

//Read the raw HTML from the request

StreamReader sr = new StreamReader(Resp.GetResponseStream(),

Encoding.Default);

//Convert the stream to a string

string s = sr.ReadToEnd();

sr.Close();

Resp.Close();

return s;

}

1 comment:

DisplayName said...

Hi! why your code works and mine doesn't. Please don't say your's is in in C# and mine is in vb that's why. LOL
Function FileUpload2() 'ByVal url As String, ByVal referer As String, ByVal filename As String
Dim r As New Random
Dim randStr = r.Next.ToString
Dim dataBoundary As String = "--xyz" + randStr
Dim req As HttpWebRequest = WebRequest.Create(url)
req.UserAgent = "Upload Test"
req.ContentType = "multipart/form-data boundary=xyz" + randStr
req.Method = "POST"
req.KeepAlive = True
Dim FileData() As Byte = File.ReadAllBytes(fileName)

Dim dataString As New StringBuilder
dataString.Append(dataBoundary & vbCrLf)
dataString.Append("Content-Disposition: form-data; name=""ul_Path""; filename=""Test123.jpg""" & vbCrLf)
dataString.Append("Content-Length: " + FileData.Length.ToString() & vbCrLf)
dataString.Append("Content-Type: image/jpeg" & vbCrLf & vbCrLf)
Dim tmpPostdata1() As Byte = System.Text.Encoding.Default.GetBytes(dataString.ToString())
MsgBox(dataString.ToString())

Dim dataString2 As New StringBuilder
dataString2.Append(dataBoundary & vbCrLf & vbCrLf)
dataString2.Append("Content-Disposition: form-data name=""MAX_FILE_SIZE"" " & vbCrLf & vbCrLf & "512000" & vbCrLf & vbCrLf)
dataString2.Append(dataBoundary + "--" & vbCrLf)
Dim tmpPostdata2() As Byte = System.Text.Encoding.Default.GetBytes(dataString2.ToString())
MsgBox(dataString2.ToString())

Dim t(tmpPostdata1.Length + FileData.Length + tmpPostdata2.Length) As Byte

tmpPostdata1.CopyTo(t, 0)
FileData.CopyTo(t, tmpPostdata1.Length)
tmpPostdata2.CopyTo(t, tmpPostdata1.Length + FileData.Length)
MsgBox(System.Text.Encoding.ASCII.GetString(t))

req.ContentLength = t.Length
req.Referer = "ul_Path"


Dim tempStream As Stream = req.GetRequestStream()
tempStream.Write(t, 0, t.Length)
tempStream.Close()

Dim resp As HttpWebResponse = req.GetResponse
Dim sr As New System.IO.StreamReader(resp.GetResponseStream(), Encoding.Default)
Dim s As String = sr.ReadToEnd()
sr.Close()
resp.Close()
Return s

End Function