C# 使用.net和sftp服务器传输大型PDF文件
我需要流大PDF(300MB-1GB)。现在我正在将它们从sftp服务器下载到C# 使用.net和sftp服务器传输大型PDF文件,c#,asp.net,.net,C#,Asp.net,.net,我需要流大PDF(300MB-1GB)。现在我正在将它们从sftp服务器下载到内存流,然后将它们流式传输到浏览器。对于小型PDF来说,它可以正常工作,但是任何大于400MB的文件都会在浏览器中打开PDF查看器,进度条会慢慢移动到100%,但PDF永远不会加载。如果我获得的内存远大于500MB,则下载时会出现SystemOutOfMemory异常。这是我的相关代码 MemoryStream memStream = new MemoryStream(); using (var client = n
内存流
,然后将它们流式传输到浏览器。对于小型PDF来说,它可以正常工作,但是任何大于400MB的文件都会在浏览器中打开PDF查看器,进度条会慢慢移动到100%,但PDF永远不会加载。如果我获得的内存远大于500MB,则下载时会出现SystemOutOfMemory
异常。这是我的相关代码
MemoryStream memStream = new MemoryStream();
using (var client = new SftpClient(sftpHost, port, username, password))
{
try
{
client.Connect();
client.ChangeDirectory(envDirectory);
using (memStream)
{
client.DownloadFile(fileName + extension, memStream);
httpResponse.Clear();
httpResponse.ClearContent();
httpResponse.ClearHeaders();
httpResponse.Buffer = true;
httpResponse.ContentType = "application/pdf";
httpResponse.AddHeader("Content-Length", memStream.Length.ToString());
memStream.WriteTo(httpResponse.OutputStream);
httpResponse.Flush();
}
}
}
---更新---
我接受了斯宾德的建议,将代码改为
using (var client = new SftpClient(sftpHost, port, username, password))
{
try
{
client.Connect();
client.ChangeDirectory(envDirectory);
httpResponse.Clear();
httpResponse.ClearContent();
httpResponse.ClearHeaders();
httpResponse.Buffer = true;
httpResponse.ContentType = "application/pdf";
client.DownloadFile(fileName + extension, httpResponse.OutputStream);
}
这消除了内存不足的异常,但较大的PDF仍然开始加载,然后在只有一个空白屏幕的情况下停止,较小的PDF仍然工作得很好
SftpClient来自client.DownloadFile(文件名+扩展名,httpResponse.OutputStream);返回void,但第二个参数是要将文件转储到的流
任何帮助,或者更好的方法都会很好 所以,
SftpClient
不是BCL的一部分,所以我猜一下,但是在您将所有标题添加到响应之后,您就不能:
client.DownloadFile(fileName + extension, httpResponse.OutputStream);
完全跳过
MemoryStream
?这是什么SftpClient
?下载文件返回什么?…ienumerable?…对象?我更新了主要帖子以回答这些问题跳过MemoryStream消除了内存不足的异常,但我仍然存在无法加载较大PDF的问题。它开始加载,然后过了一段时间加载条消失了,我留下了一个空白屏幕。