从客户端计算机上的服务器 C# Web 应用程序查看 PDF ASP.Net
View PDF from Server on Client Machine C# Web Application ASP.Net
我在 IIS 服务器上创建了一个虚拟目录(window 服务器 2012)
这让我可以访问客户端需要访问的特定文件夹中的所有 pdf 文件
然后我写了一个方法,在用户点击网格视图中的一个link之后
我写的代码如下:
将我的 C# web 应用程序发布到服务器而不是访问客户端计算机上的 web 应用程序后,我单击完成的 link 希望它会下载到客户端计算机,但它下载到服务器计算机
请帮我下载/打开虚拟文件夹或c盘上的文件到客户端。
我也把下载的pdf文件放在了下面的路径下,文件夹设置为共享:
我认为在客户端下载 pdf 可能对您有所帮助:
string filepath = @"C:\yourfile.pdf";
string filename = Path.GetFileName(filepath);
System.IO.Stream stream = null;
try
{
// Open the file into a stream.
stream = new FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
// Total bytes to read:
long bytesToRead = stream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
// Read the bytes from the stream in small portions.
while (bytesToRead > 0)
{
// Make sure the client is still connected.
if (Response.IsClientConnected)
{
// Read the data into the buffer and write into the
// output stream.
byte[] buffer = new Byte[10000];
int length = stream.Read(buffer, 0, 10000);
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
// We have already read some bytes.. need to read
// only the remaining.
bytesToRead = bytesToRead - length;
}
else
{
// Get out of the loop, if user is not connected anymore..
bytesToRead = -1;
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
// An error occurred..
}
finally
{
if (stream != null)
{
stream.Close();
}
}
我在 IIS 服务器上创建了一个虚拟目录(window 服务器 2012)
这让我可以访问客户端需要访问的特定文件夹中的所有 pdf 文件
然后我写了一个方法,在用户点击网格视图中的一个link之后
我写的代码如下:
将我的 C# web 应用程序发布到服务器而不是访问客户端计算机上的 web 应用程序后,我单击完成的 link 希望它会下载到客户端计算机,但它下载到服务器计算机
请帮我下载/打开虚拟文件夹或c盘上的文件到客户端。
我也把下载的pdf文件放在了下面的路径下,文件夹设置为共享:
我认为在客户端下载 pdf 可能对您有所帮助:
string filepath = @"C:\yourfile.pdf";
string filename = Path.GetFileName(filepath);
System.IO.Stream stream = null;
try
{
// Open the file into a stream.
stream = new FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
// Total bytes to read:
long bytesToRead = stream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
// Read the bytes from the stream in small portions.
while (bytesToRead > 0)
{
// Make sure the client is still connected.
if (Response.IsClientConnected)
{
// Read the data into the buffer and write into the
// output stream.
byte[] buffer = new Byte[10000];
int length = stream.Read(buffer, 0, 10000);
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
// We have already read some bytes.. need to read
// only the remaining.
bytesToRead = bytesToRead - length;
}
else
{
// Get out of the loop, if user is not connected anymore..
bytesToRead = -1;
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
// An error occurred..
}
finally
{
if (stream != null)
{
stream.Close();
}
}