gpt4 book ai didi

c# - Web 服务文件流到字节 [] 到 FileStream 到 pdf 失败 C#

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:09 24 4
gpt4 key购买 nike

我正在尝试创建一个 web 服务,它以 byte[] 的形式返回一个 pdf 文件,然后使用它的应用程序会抓取byte[] 并将其保存为 pdf 文件,然后打开它。文件最后打不开。

这是返回 byte[]

Web 服务
[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).

因为我没有在代码中收到错误,所以我不知道错误在哪里,即没有创建正确的文件。

名为inputStream 变量没有给出长度,所以我在这里使用了Jon Skeet 的 建议Stackoverflow:Creating a byte array from a stream

new byte[16*1024]; 

而不是

new byte[input.length]

最佳答案

错了三件事。

memoryStream.Position = 0;

在 while 循环中有问题,所以我删除了它。

其次在读取流时。它返回的是 SOAP XMl 消息,在 XXXXResult XML 标记中带有编码的 base64 字符串。所以我必须提取它。

最后我不得不使用

byte[] fileResultBytes  = Convert.FromBase64String(resultString);

SOAP 消息中提取的 resultString 中获取 byte[]。在可以在本地生成的测试 SOAP 消息中,它会告诉您此结果字符串的类型。最初我错过了。

感谢VC.OneCodeCaster的正确建议。

关于c# - Web 服务文件流到字节 [] 到 FileStream 到 pdf 失败 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56232051/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com