Jpeg displaying as base64 string in browser

G

Guest

Hi,
I'm programatically posting an image using multipart/form-data. It sends to the server OK, but when I try to view it in the browser, it is still in the base64 string I sent it as: /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUG etc....

I've been through the form I built over and over:

string PostData = "";

PostData += "-----------------------------7d41fb3081216";
PostData += "\r";
PostData += "\n";

PostData += "Content-Disposition: form-data; name=\"userfile\"; filename=" + "\"c:\\temp\\" + "myimg.jpg\";";
PostData += "\r";
PostData += "\n";

PostData += "Content-Type: image/jpeg; name=\"myimg.jpg\";";
PostData += "\r";
PostData += "\n";

PostData +="Content-Transfer-Encoding: binary;";
PostData += "\r";
PostData += "\n";
PostData += "\r";
PostData += "\n";

string image_string = Convert.ToBase64String(documentcontents);
PostData += image_string;

PostData += "\r";
PostData += "\n";

PostData += "-----------------------------7d41fb3081216--";

This is the how i'm reading the image in:

string strdocPath;
strdocPath = "c:\\temp\\" + "myimg.jpg";

FileStream objfilestream = new FileStream(strdocPath,FileMode.Open,FileAccess.Read);
int len = (int)objfilestream.Length;
Byte[] documentcontents = new Byte[len];
objfilestream.Read(documentcontents,0,len);
objfilestream.Close();


I've tried pretty much every order of mimes, but still the image isn't sent properly. Does anyone have any experience of this or what could be causing the base64 string to show like that?

Thanks
 
J

Jon Skeet [C# MVP]

Magnus said:
I'm programatically posting an image using multipart/form-data. It
sends to the server OK, but when I try to view it in the browser, it
is still in the base64 string I sent it as:
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUG etc....

I've been through the form I built over and over:

string PostData = "";

PostData += "-----------------------------7d41fb3081216";
PostData += "\r";
PostData += "\n";

You should really be using a StringBuilder for this - you're creating
laods of extra strings.
PostData += "Content-Disposition: form-data; name=\"userfile\"; filename=" + "\"c:\\temp\\" + "myimg.jpg\";";
PostData += "\r";
PostData += "\n";

PostData += "Content-Type: image/jpeg; name=\"myimg.jpg\";";
PostData += "\r";
PostData += "\n";

PostData +="Content-Transfer-Encoding: binary;";

What have you specified binary for the Content-Transfer-Encoding when
you're actually using Base64? I suspect that's the problem.
PostData += "\r";
PostData += "\n";
PostData += "\r";
PostData += "\n";

string image_string = Convert.ToBase64String(documentcontents);
PostData += image_string;

PostData += "\r";
PostData += "\n";

PostData += "-----------------------------7d41fb3081216--";

This is the how i'm reading the image in:

string strdocPath;
strdocPath = "c:\\temp\\" + "myimg.jpg";

FileStream objfilestream = new FileStream(strdocPath,FileMode.Open,FileAccess.Read);
int len = (int)objfilestream.Length;
Byte[] documentcontents = new Byte[len];
objfilestream.Read(documentcontents,0,len);
objfilestream.Close();

That's an unsafe way of reading a file, as well: you're not closing the
file if there's an exception, and you're assuming that Read will return
all the data.

See http://www.pobox.com/~skeet/csharp/readbinary.html for more info.
 
G

Guest

Jon Thanks, I'm using your ReadFully function now. In addition I was making two mistakes:
I was using the GetBytes method instead of Encoding.Default.GetString to convert my file buffer,
and secondly I was posting the ContentLength of PostData, rather than theContentLength of a Byte array that is converted with GetBytes. It works now.

Jon Skeet said:
Magnus said:
I'm programatically posting an image using multipart/form-data. It
sends to the server OK, but when I try to view it in the browser, it
is still in the base64 string I sent it as:
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAUDBAQEAwUEBAQFBQUG etc....

I've been through the form I built over and over:

string PostData = "";

PostData += "-----------------------------7d41fb3081216";
PostData += "\r";
PostData += "\n";

You should really be using a StringBuilder for this - you're creating
laods of extra strings.
PostData += "Content-Disposition: form-data; name=\"userfile\"; filename=" + "\"c:\\temp\\" + "myimg.jpg\";";
PostData += "\r";
PostData += "\n";

PostData += "Content-Type: image/jpeg; name=\"myimg.jpg\";";
PostData += "\r";
PostData += "\n";

PostData +="Content-Transfer-Encoding: binary;";

What have you specified binary for the Content-Transfer-Encoding when
you're actually using Base64? I suspect that's the problem.
PostData += "\r";
PostData += "\n";
PostData += "\r";
PostData += "\n";

string image_string = Convert.ToBase64String(documentcontents);
PostData += image_string;

PostData += "\r";
PostData += "\n";

PostData += "-----------------------------7d41fb3081216--";

This is the how i'm reading the image in:

string strdocPath;
strdocPath = "c:\\temp\\" + "myimg.jpg";

FileStream objfilestream = new FileStream(strdocPath,FileMode.Open,FileAccess.Read);
int len = (int)objfilestream.Length;
Byte[] documentcontents = new Byte[len];
objfilestream.Read(documentcontents,0,len);
objfilestream.Close();

That's an unsafe way of reading a file, as well: you're not closing the
file if there's an exception, and you're assuming that Read will return
all the data.

See http://www.pobox.com/~skeet/csharp/readbinary.html for more info.
 
J

Jon Skeet [C# MVP]

Magnus said:
Jon Thanks, I'm using your ReadFully function now. In addition I was
making two mistakes: I was using the GetBytes method instead of
Encoding.Default.GetString to convert my file buffer

Where are you converting the file buffer?
and secondly I
was posting the ContentLength of PostData, rather than
theContentLength of a Byte array that is converted with GetBytes. It
works now.

Good - but I'm still slightly worried about using Encoding.Default
anywhere...
 
G

Guest

I admit that it's more by trial and error, than knowledge that I got it working:

byte [] b = ReadFully(str,(int)str.Length);

string image_data=Encoding.Default.GetString(b); //here's what I mean by converting.
str.Close();

PostData += image_data;

//other form info etc..
//..

//write it out

Byte [] Buffer = Encoding.Default.GetBytes(PostData);
Request.ContentLength=Buffer.Length;

Stream newStream = Request.GetRequestStream();

newStream.Write(Buffer, 0, Buffer.Length);
newStream.Flush();
newStream.Close();

//get response stream ..
 
J

Jon Skeet [C# MVP]

Magnus said:
I admit that it's more by trial and error, than knowledge that I got it working:

byte [] b = ReadFully(str,(int)str.Length);

string image_data=Encoding.Default.GetString(b); //here's what I mean by converting.
str.Close();

PostData += image_data;

//other form info etc..
//..

//write it out

Byte [] Buffer = Encoding.Default.GetBytes(PostData);
Request.ContentLength=Buffer.Length;

Stream newStream = Request.GetRequestStream();

newStream.Write(Buffer, 0, Buffer.Length);
newStream.Flush();
newStream.Close();

//get response stream ..

Ah. That's bad news. What you should do instead is avoid *ever*
encoding or decoding the binary data. Write the text using
Encoding.ASCII.GetBytes (...) to get the byte array for each part, and
then just write the image data directly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top