如何使用vb .net在服务器上压缩文件夹?

时间:2015-08-23 15:09:26

标签: .net vb.net zip directory

我正在创建一个按钮,当用户点击按钮时,我想压缩文件夹并下载它。

我的代码在本地主机上工作得很好但是当我将它移动到服务器时,我得到的错误。

以下是错误:

Server Error in '/myapp' Application.
Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine. 

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

<!-- Web.Config Configuration File -->
<configuration>
<system.web>
    <customErrors mode="Off"/>
</system.web>
</configuration>

Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.

<!-- Web.Config Configuration File -->
<configuration>
<system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>

下面你会找到我的zip和下载文件夹的代码。我还添加了一个system.io.compression作为参考。

aspx.vb代码:

Imports System.IO
Imports System.IO.Compression

' Inorder to use 'ZipFile', you first have to 'Add reference'
' Right Click project > Add Reference > Browse > "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.IO.Compression.FileSystem\..." > ok

Partial Class myclass
Inherits System.Web.UI.Page

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click

    Dim folderPath As String = "C:\user\dave\desktop\MyFolder"

    ' Create Zip folder
    Dim TempFile = System.IO.Path.GetTempFileName() + ".zip"
        System.IO.Compression.ZipFile.CreateFromDirectory(folderPath, TempFile)

        ' Download Zip folder
        Response.Buffer = False
        Response.Clear()
        Response.AddHeader("content-disposition", "attachment;filename=Cases.zip")
        Response.ContentType = "Application/zip"
        Response.TransmitFile(TempFile)
        Response.End()
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

您似乎已对文件夹路径进行了硬编码:

Dim folderPath As String = "C:\user\dave\desktop\MyFolder"

因此,您可以查看以下几项内容:

  • 此文件夹实际存在于部署应用程序的服务器上
  • 运行应用程序池的标识具有对此文件夹的读取权限

要查明问题,您只需按照错误消息中的建议操作,并在web.config中启用详细错误输出:

<system.web>
    <customErrors mode="Off" />
</system.web>