gpt4 book ai didi

asp.net - 从 web api 服务下载 pdf 文件的问题

转载 作者:行者123 更新时间:2023-12-05 05:26:49 24 4
gpt4 key购买 nike

我正在尝试设置一个 Web API 服务,该服务在目录中搜索 .pdf 文件并在找到时返回该文件。

Controller

public class ProductsController : ApiController
{

[HttpPost]
public HttpResponseMessage Post([FromBody]string certificateId)
{
string fileName = certificateId + ".pdf";
var path = @"C:\Certificates\20487A" + fileName;

//check the directory for pdf matching the certid
if (File.Exists(path))
{
//if there is a match then return the file
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(path, FileMode.Open);
stream.Position = 0;
result.Content = new StreamContent(stream);
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = fileName };
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
result.Content.Headers.ContentDisposition.FileName = fileName;
return result;
}
else
{
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.Gone);
return result;
}
}
}

我正在使用以下代码调用该服务

private void GetCertQueryResponse(string url, string serial)
{
string encodedParameters = "certificateId=" + serial.Replace(" ", "");

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);

httpRequest.Method = "POST";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.AllowAutoRedirect = false;

byte[] bytedata = Encoding.UTF8.GetBytes(encodedParameters);
httpRequest.ContentLength = bytedata.Length;

Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
{

byte[] bytes = null;
using (Stream stream = response.GetResponseStream())
using (MemoryStream ms = new MemoryStream())
{
int count = 0;
do
{
byte[] buf = new byte[1024];
count = stream.Read(buf, 0, 1024);
ms.Write(buf, 0, count);
} while (stream.CanRead && count > 0);
ms.Position = 0;
bytes = ms.ToArray();
}

var filename = serial + ".pdf";

Response.ContentType = "application/pdf";
Response.Headers.Add("Content-Disposition", "attachment; filename=\"" + filename + "\"");
Response.BinaryWrite(bytes);
}
}

从下载文件对话框显示正确的文件名和大小等的意义上来看,这似乎是有效的,但下载只需要几秒钟(当文件大小 >30mb 时)并且文件已损坏当我尝试打开它们时。

知道我做错了什么吗?

最佳答案

您的代码看起来与我过去使用的类似,但下面是我通常使用的代码:

    Response.AddHeader("content-length", myfile.Length.ToString())
Response.AddHeader("content-disposition", "inline; filename=MyFilename")
Response.AddHeader("Expires", "0")
Response.AddHeader("Pragma", "Cache")
Response.AddHeader("Cache-Control", "private")

Response.ContentType = "application/pdf"

Response.BinaryWrite(finalForm)

我发布这个有两个原因。第一,添加内容长度 header ,您可能必须指示文件有多大,以便应用程序等待整个响应。

如果那不能解决问题。设置一个断点,字节数组内容是否有适当的长度(也就是 30 MB 文件的 3000 万字节)?您是否使用 fiddler 查看通过 HTTP 调用返回了多少内容?

关于asp.net - 从 web api 服务下载 pdf 文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23568098/

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