Author |
Message
|
johgri |
Posted: Sat Nov 09, 2013 7:46 am Post subject: Send ascii file through C# code |
|
|
Newbie
Joined: 09 Nov 2013 Posts: 2
|
Hi,
I have searched in the internet and I have written a sample code in c# for sending an ascii file through MQ. But all I have found are about sending a string and not a file. I have written the following code, using streamfile and fill a string variable named "text". Then I use this variable to send it through MQ queue :
public void GetFile()
{
String path = "C:\\file.txt";
StreamReader streamReader = new StreamReader(path);
text = streamReader.ReadToEnd();
streamReader.Close();
}
public void PutMessageOnQueue(string message)
{
try
{
message = text;
queue = queueManager.AccessQueue(queueName, MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);
queueMessage = new MQMessage();
queueMessage.WriteString(message);
queueMessage.Format = MQC.MQFMT_STRING;
queue.Put(queueMessage);
}
catch (MQException mqexp)
{
Console.WriteLine("MQSeries Exception: " + mqexp.Message);
Console.WriteLine("MQSeries Exception: " + mqexp.Reason);
Console.WriteLine("MQSeries Exception: " + mqexp.StackTrace);
}
}
Can you please help me with the code?
Regards,
John |
|
Back to top |
|
 |
mqjeff |
Posted: Sat Nov 09, 2013 9:36 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
A file is just a string of bytes. |
|
Back to top |
|
 |
gbaddeley |
Posted: Sun Nov 10, 2013 2:11 pm Post subject: |
|
|
 Jedi Knight
Joined: 25 Mar 2003 Posts: 2538 Location: Melbourne, Australia
|
Your code design is correct. Read the entire file into a string (or some other suitable buffer type, eg. byte array). Put the string as an MQ message. _________________ Glenn |
|
Back to top |
|
 |
johgri |
Posted: Tue Nov 19, 2013 3:48 pm Post subject: |
|
|
Newbie
Joined: 09 Nov 2013 Posts: 2
|
Hello,
Message is written in the queue, but although the ascii file has 5 lines, MQ queue writes the data in one single line. I was expected to have a message with 5 lines separated by carriage return character.
I have changed C# code using an array:
mqMsg = new MQMessage();
string[] lines = System.IO.File.ReadAllLines(@"C:\\Users\\vdt003.txt");
foreach (string line in lines)
{
message = line ;
msgLen = message.Length;
if (msgLen > 0)
.................
}
Can you please help?
Thanks John |
|
Back to top |
|
 |
bruce2359 |
Posted: Tue Nov 19, 2013 3:56 pm Post subject: |
|
|
 Poobah
Joined: 05 Jan 2008 Posts: 9469 Location: US: west coast, almost. Otherwise, enroute.
|
If you imbed CR/LF characters in the data you put to the queue, they will be there when you get the message out of the queue. Messages are not files, and therefore to not adhere to file protocols like CR/LF. _________________ I like deadlines. I like to wave as they pass by.
ב''ה
Lex Orandi, Lex Credendi, Lex Vivendi. As we Worship, So we Believe, So we Live. |
|
Back to top |
|
 |
mqjeff |
Posted: Wed Nov 20, 2013 3:13 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
An MQ message is just a string of bytes.
A string of bytes doesn't know that 0x0a0x0d is a "line ending". |
|
Back to top |
|
 |
gbaddeley |
Posted: Wed Nov 20, 2013 2:33 pm Post subject: |
|
|
 Jedi Knight
Joined: 25 Mar 2003 Posts: 2538 Location: Melbourne, Australia
|
johgri wrote: |
string[] lines = System.IO.File.ReadAllLines(@"C:\\Users\\vdt003.txt"); |
You need to read the entire file into a single string as one entity, not as a sequence of records (appended together without line terminators). _________________ Glenn |
|
Back to top |
|
 |
|