我想向.net c# component发送一个二进制文件,格式如下
<BinaryFileString fileType='pdf'>
<!--binary file data string here-->
</BinaryFileString>在被调用的组件中,我将使用上面的xml字符串,并将在BinaryFileString标记中接收到的二进制字符串转换为filetype='‘属性指定的文件。文件类型可以是doc/pdf/xls/rtf
我在调用应用程序中有从要发送的文件中提取字节的代码。如何准备使用xml标记发送它?我希望应用程序向组件发送一个字符串,而不是字节流。这是因为我无法仅通过查看字节流来破译pdf/doc/xls文件类型。因此使用带有filetype属性的xml字符串。对此有什么想法吗?
以下字节的提取方法
FileStream fs = new FileStream(_filePath, FileMode.Open, FileAccess.Read);
using (Stream input = fs)
{
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{}
}
return buffer;谢谢。
编辑:
只是为了澄清我为什么要使用xml字符串,而不是在组件上设置属性。实际上,我的调用应用程序正在尝试模拟Siebel如何调用我的组件。http://download.oracle.com/docs/cd/E05553_01/books/eScript/eScript_JSReference244.html#wp1014380我不确定Siebel是否可以按照我的需要设置我的组件属性。因此,我从它的角度出发,以xml格式发送数据。
发布于 2010-05-31 13:39:02
Base64表示法通常用于表示二进制数据。
public void EncodeWithString() {
System.IO.FileStream inFile;
byte[] binaryData;
try {
inFile = new System.IO.FileStream(inputFileName,
System.IO.FileMode.Open,
System.IO.FileAccess.Read);
binaryData = new Byte[inFile.Length];
long bytesRead = inFile.Read(binaryData, 0,
(int)inFile.Length);
inFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or reading from it.
System.Console.WriteLine("{0}", exp.Message);
return;
}
// Convert the binary input into Base64 UUEncoded output.
string base64String;
try {
base64String =
System.Convert.ToBase64String(binaryData,
0,
binaryData.Length);
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("Binary data array is null.");
return;
}
// Write the UUEncoded version to the XML file.
System.IO.StreamWriter outFile;
try {
outFile = new System.IO.StreamWriter(outputFileName,
false,
System.Text.Encoding.ASCII);
outFile.Write("<BinaryFileString fileType='pdf'>");
outFile.Write(base64String);
outFile.Write("</BinaryFileString>");
outFile.Close();
}
catch (System.Exception exp) {
// Error creating stream or writing to it.
System.Console.WriteLine("{0}", exp.Message);
}
}在接收端,您可以反转并返回原始文件内容,如下所述。
// Convert the Base64 UUEncoded input into binary output.
byte[] binaryData;
try {
binaryData =
System.Convert.FromBase64String(base64String);
}
catch (System.ArgumentNullException) {
System.Console.WriteLine("Base 64 string is null.");
return;
}
catch (System.FormatException) {
System.Console.WriteLine("Base 64 string length is not " +
"4 or is not an even multiple of 4." );
return;
}发布于 2010-05-31 11:52:27
你能BASE64你的字节吗?参考:Convert.ToBase64,Convert.FromBase64String
发布于 2010-05-31 12:09:56
在@russau's answer上扩展它的工作方式如下:
var s = "Hello World";
var b = Encoding.Default.GetBytes(s);
var bstr = Convert.ToBase64String(b);
Console.WriteLine("Original String:" + s);
Console.WriteLine("Base64 String:" + bstr);
var fromBStr = Convert.FromBase64String(bstr);
var st = Encoding.Default.GetString(fromBStr);
Console.WriteLine("Converted string: " + st);您不需要前两行:
var s = "Hello World";
var b = Encoding.Default.GetBytes(s);因为你已经有了一个字节数组。我使用了字符串来显示,当您从Convert.FromBase64String转换字节数组时,最终得到的值与开始时的值完全相同
https://stackoverflow.com/questions/2941255
复制相似问题