473,805 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Jpeg displaying as base64 string in browser

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/4AAQSkZJRgABAQA AAQABAAD/2wBDAAUDBAQEAwU EBAQFBQUG 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.jp g\";";
PostData += "\r";
PostData += "\n";

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

string image_string = Convert.ToBase6 4String(documen tcontents);
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(strd ocPath,FileMode .Open,FileAcces s.Read);
int len = (int)objfilestr eam.Length;
Byte[] documentcontent s = new Byte[len];
objfilestream.R ead(documentcon tents,0,len);
objfilestream.C lose();
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
Jul 21 '05 #1
5 3235
Magnus <Ma****@discuss ions.microsoft. com> wrote:
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/4AAQSkZJRgABAQA AAQABAAD/2wBDAAUDBAQEAwU EBAQFBQUG 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.jp g\";";
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.ToBase6 4String(documen tcontents);
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(strd ocPath,FileMode .Open,FileAcces s.Read);
int len = (int)objfilestr eam.Length;
Byte[] documentcontent s = new Byte[len];
objfilestream.R ead(documentcon tents,0,len);
objfilestream.C lose();


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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
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.Defaul t.GetString to convert my file buffer,
and secondly I was posting the ContentLength of PostData, rather than theContentLengt h of a Byte array that is converted with GetBytes. It works now.

"Jon Skeet [C# MVP]" wrote:
Magnus <Ma****@discuss ions.microsoft. com> wrote:
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/4AAQSkZJRgABAQA AAQABAAD/2wBDAAUDBAQEAwU EBAQFBQUG 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.jp g\";";
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.ToBase6 4String(documen tcontents);
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(strd ocPath,FileMode .Open,FileAcces s.Read);
int len = (int)objfilestr eam.Length;
Byte[] documentcontent s = new Byte[len];
objfilestream.R ead(documentcon tents,0,len);
objfilestream.C lose();


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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
Magnus <Ma****@discuss ions.microsoft. com> wrote:
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.Defaul t.GetString to convert my file buffer
Where are you converting the file buffer?
and secondly I
was posting the ContentLength of PostData, rather than
theContentLengt h of a Byte array that is converted with GetBytes. It
works now.


Good - but I'm still slightly worried about using Encoding.Defaul t
anywhere...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
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=Enco ding.Default.Ge tString(b); //here's what I mean by converting.
str.Close();

PostData += image_data;

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

//write it out

Byte [] Buffer = Encoding.Defaul t.GetBytes(Post Data);
Request.Content Length=Buffer.L ength;

Stream newStream = Request.GetRequ estStream();

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

//get response stream ..

"Jon Skeet [C# MVP]" wrote:
Magnus <Ma****@discuss ions.microsoft. com> wrote:
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.Defaul t.GetString to convert my file buffer


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


Good - but I'm still slightly worried about using Encoding.Defaul t
anywhere...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #5
Magnus <Ma****@discuss ions.microsoft. com> wrote:
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=Enco ding.Default.Ge tString(b); //here's what I mean by converting.
str.Close();

PostData += image_data;

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

//write it out

Byte [] Buffer = Encoding.Defaul t.GetBytes(Post Data);
Request.Content Length=Buffer.L ength;

Stream newStream = Request.GetRequ estStream();

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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
26407
by: Rodney Pont | last post by:
I've got the example below to set up phpOpenTracker to log exit URL's but I'm having trouble getting it to work. I have played with the quotes and changed the \\2 to $3 and got the url in there but I can't get it to base64 encode it. I'm new to PHP and any help on getting the encoding to work would be appreciated. <?php function encode_exit_urls($buffer) {
2
1297
by: Dennis Allen | last post by:
Hi. When displaying a jpeg file, the image shows up in the top left corner of the browser. Any way to get the image centered?
4
5367
by: John | last post by:
Hi all, I've been going through google and yahoo looking for a certain base64 decoder in C without success. What I'm after is something that you can pass a base64 encoded string into and get back a decoded String. Any help is very much appreciated. Thanks Philip.
0
2768
by: Johann Blake | last post by:
In my need to decode a JPEG 2000 file, I discovered like many that there was no functionality for this in the .NET Framework. Instead of forking out a pile of cash to do this, I came up with the idea that costs nothing and it is inheritently built into the Framework. So here is the solution... When you use the WebRequest and WebResponse classes to obtain graphics from a web site, these classes have built-in decoding for JPEG 2000 files....
5
311
by: Magnus | last post by:
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";
15
9815
by: mleaver | last post by:
I want to open a second window and display a binary image that is returned from a java program via XMLRPC. The data returned is a binary encoded base64 png file. If I write the data out to a file on my server, I can display it using the following javascript: var windowHandle = window.open('about:blank','windowName','width=250,height=250'); windowHandle.document.write('<img name="myImage" src="images/test.png">');
11
4774
by: prats | last post by:
I want to write a GUI application in PYTHON using QT. This application is supposed to take in Japanese characters. I am using PyQt as the wrapper for using QT from python. I am able to take input in japanese. But I am unable to display them back to GUI. It displays some junk characters Can anyone suggest me some way how to debug the issue. The code used for tranferring data from view to document is: " codec =...
3
18859
by: Kuldeep | last post by:
Hi All, Could you please give me some guidelines on dealing with Base64 encoded string. The actual purpose is to decode Base64 Encoded string and stream the data to a browser so that it can be viewed in a web application Regards, Kuldeep
1
1132
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am developing an application in asp.net, Visual C# (background) and ATL COM Component. I am using com component in asp.net page code behind (like default.aspx.cs). COM component is returning binary data in string. It has binary including null, spaces and some special characters also.
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.