我在尝试从我的asp.net页面运行以下代码时遇到了问题。 代码来自另一篇文章(https://dev59.com/Rm025IYBdhLWcg3wvIko 当Test.pdf文件位于我的本地驱动器中时,单击对话框上的打开按钮后无法打开该文件。 当单击保存按钮时,文件以“C:\ Downloads”中的myAspPageName.pdf尝试保存,操作将永远进行而实际上不保存任何内容。
我确定我做错了什么。 感谢任何帮助。 是否有更好的方法来完成同样的事情?
我确定我做错了什么。 感谢任何帮助。 是否有更好的方法来完成同样的事情?
protected void Page_Load(object sender, EventArgs e)
{
Context.Response.Buffer = false;
FileStream inStr = null;
byte[] buffer = new byte[1024];
long byteCount; inStr = File.OpenRead(@"C:\Downloads\Test.pdf");
while ((byteCount = inStr.Read(buffer, 0, buffer.Length)) > 0)
{
if (Context.Response.IsClientConnected)
{
Context.Response.ContentType = "application/pdf";
Context.Response.OutputStream.Write(buffer, 0, buffer.Length);
Context.Response.Flush();
}
}
}