繁体   English   中英

在客户端计算机C#Web应用程序ASP.Net上从服务器查看PDF

[英]View PDF from Server on Client Machine C# Web Application ASP.Net

我在IIS服务器(窗口服务器2012)上创建了一个虚拟目录,该目录使我可以访问客户端需要访问的特定文件夹中的所有pdf文件。 在此处输入图片说明

然后,我编写了一种方法,在用户单击网格视图中的链接后 在此处输入图片说明

我写的代码如下: 在此处输入图片说明

在此处输入图片说明

在将C#Web应用程序发布到服务器而不是在客户端计算机上访问Web应用程序之后,我单击完整的链接,希望它可以下载到客户端计算机,但可以下载到服务器计算机。 在此处输入图片说明

请帮助我将虚拟文件夹或c驱动器上的文件下载/打开到客户端计算机。

我还将下载的pdf文件放在以下路径中,并将文件夹设置为shared: 在此处输入图片说明

我认为这可能对您在客户端下载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();
                }
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM