views:

63

answers:

2

I am having difficulties figuring out what is causing wierd characters to appear in my output stream ... full code @pastebin

Fiddler output

notice the "s", "X", "" before my boundary?

s---------------634227387532666996
Content-Disposition: form-data; name='key'

c06f4d0cdf6f2cc652635a08be34973d
X---------------634227387532666996
Content-Disposition: form-data; name='type'

file
�---------------634227387532666996
Content-Disposition: form-data; name='image'; filename='application_osx_split.png'
Content-Type=image/png

�PNG

my code

var bound = "-------------" + DateTime.Now.Ticks.ToString();
var tmplField = "--" + bound + "\r\nContent-Disposition: form-data; name='{0}'\r\n\r\n{1}\r\n";
var tmplFile = "--" + bound + "\r\nContent-Disposition: form-data; name='{0}'; filename='{1}'\r\nContent-Type={2}\r\n\r\n";

....

using (var reqStream = req.GetRequestStream())
{
    var reqWriter = new BinaryWriter(reqStream);

    reqWriter.Write(string.Format(tmplField, "key", "c06f4d0cdf6f2cc652635a08be34973d"));
    reqWriter.Write(string.Format(tmplField, "type", "file"));
    reqWriter.Write(string.Format(tmplFile, "image", Path.GetFileName(filepath), "image/" + Path.GetExtension(filepath).Substring(1)));
    reqWriter.Write(File.ReadAllBytes(filepath));
    reqWriter.Write("\r\n--" + bound + "--");
    reqWriter.Flush();
}

UPDATE

I noticed that if i did something like below instead, using a combination of Stream & Binary Writers, I can avoid the problem. Why is this so?

var reqWriter = new StreamWriter(reqStream);
reqWriter.Write(string.Format(tmplField, "key", "c06f4d0cdf6f2cc652635a08be34973d"));
reqWriter.Write(string.Format(tmplField, "type", "file"));
reqWriter.Write(string.Format(tmplFile, "image", Path.GetFileName(filepath), "image/" + Path.GetExtension(filepath).Substring(1)));
reqWriter.Flush();

var binWriter = new BinaryWriter(reqStream);
binWriter.Write(File.ReadAllBytes(filepath));
binWriter.Write("\r\n--" + bound + "--");
binWriter.Flush();
+4  A: 

BinaryWriter prefixes strings with their length.

Use a StreamWriter instead.

dtb
Oh, then can I use a `StreamWriter` to write `byte[]` too? Look at the `File.ReadAllBytes(filepath)` part, thats the only reason why I used `BinaryWriter` later on when I discovered `StreamWriter` can;t write `byte[]`
jiewmeng
Sorry, I didn't note the byte[]. In this case you need to convert the strings to byte[] using UTF8Encoding and write the byte[] directly to the stream.
dtb
Alternatively, you can (or maybe even need to?) encode the file contents as Base64. The result is a string. See http://stackoverflow.com/questions/1853803/is-there-a-popular-c-library-for-working-http-eg-simplifing-working-with-httpwe/1853909#1853909
dtb
what if I don't need to convert to base64 how do I use a normal `StreamWriter` to write `byte[]`
jiewmeng
by the way, if I did the base64 encoding, will my image be `image/png` or something else? I think my request is marked in red in fiddler means something wrong with the request headers etc.
jiewmeng
A: 

You can also check out the multiplart form upload helper using webclient

feroze