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.Rea d);
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 5 3188
Magnus <Ma****@discussions.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/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.Rea d); 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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 [C# MVP]" wrote: Magnus <Ma****@discussions.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/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.Rea d); 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.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Magnus <Ma****@discussions.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.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...
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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 ..
"Jon Skeet [C# MVP]" wrote: Magnus <Ma****@discussions.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.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...
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Magnus <Ma****@discussions.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=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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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?
|
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...
|
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...
|
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:...
|
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...
|
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...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |