c# - Web服务文件流到字节[]到文件流到pdf失败C#
问题描述
我正在尝试创建一个将文件web service
返回为 a的应用程序,然后使用它的应用程序抓取并将其保存为文件,然后打开它。该文件最终无法打开。pdf
byte[]
byte[]
pdf
这是返回一个byte[]
[WebMethod]
public byte[] XXXX(int fileID)
{
try
{
using (EntitiesModel dbContext = new EntitiesModel())
{
string fileFullPath = .....
.......
if (fileFullNamePath != null)
{
FileStream fileStream = new FileStream(fileFullNamePath, FileMode.Open, System.IO.FileAccess.Read);
int len = fileStream.Length.ToInt();
Byte[] documentContents = new byte[len];
fileStream.Read(documentContents, 0, len);
fileStream.Close();
return documentContents;
然后使用以下代码从应用程序调用它
string soap = "<?xml version=\"1.0\" encoding=\"utf - 8\"?>" +
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<soap:Body>" +
"<XXXX xmlns=\"http://tempuri.org/\">" +
"<fileID>XXXXX</fileID>" +
"</XXXX>" +
"</soap:Body>" +
"</soap:Envelope>";
string localhostContext = @"http://localhost:3381/";
string webserviceAddress = @"XXXX/XXXX/XXXXX.asmx";
string url = localhostContext + webserviceAddress ;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "text/xml";
request.ContentLength = soap.Length;
request.Timeout = 20000;
request.Method = "POST";
using (Stream stream = request.GetRequestStream())
{
using (StreamWriter streamWriter = new StreamWriter(stream))
{
streamWriter.Write(soap); }
}
}
byte[] bytes;
try
{
WebResponse response = request.GetResponse();
bytes = ReadFully(response.GetResponseStream());
}
catch (Exception exception)
{
throw;
}
private byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16*1024];
using (MemoryStream memoryStream = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Position = 0;
memoryStream.Write(buffer, 0, read);
}
return memoryStream.ToArray();
}
}
FileStream objfilestream =
new FileStream(fileName, FileMode.Create,FileAccess.ReadWrite);
objfilestream.Write(bytes, 0, bytes.Length);
objfilestream.Close();
var process = Process.Start(fileName);
代码运行良好并创建一个pdf
然后尝试打开它pdf
。但是文件无法打开。Adobe Acrobat 给出错误
Adobe Acrobat Reader could not open XXX.pdf because it is either not a
supported file type or because the file has been damaged (for example, it
was sent as an email attachment and wasn't correctly decoded).
因为我没有在代码中收到错误,所以我不知道错误在哪里,没有创建正确的文件。
Stream
调用的变量有一个问题input
没有给出长度,所以我Jon Skeet's
在这里使用了建议Stackoverflow:Creating a byte array from a stream
new byte[16*1024];
而不是
new byte[input.length]
解决方案
错了三件事。
memoryStream.Position = 0;
在 while 循环中有问题,所以我将其删除。
其次,在阅读流时。它返回的是带有XXXXResult标签SOAP
XMl
中编码字符串的消息。所以我不得不提取它。base64
XML
最后我不得不使用
byte[] fileResultBytes = Convert.FromBase64String(resultString);
从消息中提取的 resultString 中获取 byte[] SOAP
。SOAP
在可以本地生成的测试消息中,它会告诉您此结果字符串的类型。我最初错过了这一点。
感谢他们VC.One
的CodeCaster
正确建议。
推荐阅读
- clojurescript - 在 core.cljs Clojure 脚本中访问 HTML Input 元素
- c - CS50 Pset4 Recover:正确恢复 049.jpg,恢复的图像不匹配
- java - Spring boot:CsvReader 类未正确从 application.properties 读取文件名
- c# - 文件访问和 Parallel.For
- python - 在 SMTP lib 函数中需要帮助
- expo - 由于套接字超时错误,无法安装 expo-cli
- macos - setenv 出错:MATLAB 上的 startup.m 文件“不存在或不可读”
- vue-router - 如何使用“this.$router.push()”传递道具对象
- reactjs - react-mathjax 渲染两次。我该如何解决?
- c - 在switch语句之前输入字符串并使用switch语句比较c中的字符串