【问题标题】:How to zip a folder on server using vb .net?如何使用 vb .net 压缩服务器上的文件夹?
【发布时间】:2015-11-17 00:59:58
【问题描述】:

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

我的代码在本地主机上运行良好,但是当我将它移动到服务器时却出现错误。

这是错误:

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

【问题讨论】:

    标签: .net vb.net zip directory


    【解决方案1】:

    您似乎已经硬编码了文件夹路径:

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

    因此,您可以检查以下几点:

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

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

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

    【讨论】:

      猜你喜欢
      • 2010-10-12
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      相关资源
      最近更新 更多