如何在.Net 中从内网下载PDF 文件?

How to download PDF file from intranet in .Net?

问题作为标题。

我想要一个按钮,点击它会下载存储在其他 Intranet 服务器上的 pdf 文件。

我尝试使用下面的代码从其他内网文件下载pdf文件。

Response.Clear()
Response.ContentType = "application/pdf"
Response.AppendHeader("Content-Disposition", "attachment; filename=Test.pdf")
Response.BinaryWrite(System.IO.File.ReadAllBytes("\intranetServer\Test.pdf"))
Response.End()

不知道是代码问题还是别的什么?

也许 IIS 需要设置一些配置?

我不知道要实现它。

如何使用 My.Computer.Network.DownloadFile

例如你可以这样做:

My.Computer.Network.DownloadFile("\intranetServer\Test.pdf","Path\Folder\file.pdf")

否则你可以使用System.Net.WebClient

这会让您跟踪进度并有更多的控制权,例如:

Dim wc As System.Net.WebClient
wc = New System.Net.WebClient()
wc.DownloadFileAsync(New Uri("\intranetServer\Test.pdf";), "Path\Folder\file.pdf")

PS:我不确定您可能需要的 Intranet URI 的结构 Net.Tcp://

如果您想保留您的解决方案,请尝试使用它并记住您需要确保 IIS 对每个人都可见并正确构造 Uri 可能需要使用 file://

Private Sub WriteFile(filepath As String, fileName As String, contentType As String, response As HttpResponse)
    response.Buffer = True
    response.Clear()
    response.ContentType = contentType
    response.Charset = "iso-8859-1"
    response.HeaderEncoding = System.Text.Encoding.GetEncoding("iso-8859-1")

    Dim extension As String = System.IO.Path.GetExtension(fileName)
    response.AddHeader("content-disposition", Convert.ToString("attachment; filename=") & fileName)

    response.WriteFile(filepath)
    response.Flush()
End Sub