首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VB.NEt不能将压缩文件流/下载到客户端

VB.NEt不能将压缩文件流/下载到客户端
EN

Stack Overflow用户
提问于 2021-11-24 12:52:45
回答 2查看 139关注 0票数 0

我有一个PDF字符串列表,字符串的形式为"http://whatever.pdf“,需要创建一个压缩文件,并将其作为下载文件流到客户端。

奇怪的是,如果我创建zip (我使用ZipOutputStream)并将文件写入磁盘,它就能工作,我能够打开生成的zip文件并将其解压缩,没有问题,但是如果我流它(我需要做的),我会得到一个相同大小的zip文件,但是在尝试打开它时会出错。

CreateZip:

代码语言:javascript
复制
Private Function CreateZip(pdfPathList As List(Of String), fileRoot As String) As String

    Response.Clear()
    Response.BufferOutput = False
    Response.ContentType = "application/zip"
    Response.AddHeader("content-disposition", "attachment; filename=pdf.zip")

    Dim pdfPathListLocal As List(Of String) = Utility.DownloadFilesFromList(pdfPathList, fileRoot)
    Dim outputMemStream = Utility.GenerateZipOutpuStream(pdfPathListLocal)
    Dim zipName As String = Guid.NewGuid.ToString() & ".zip"
    outputMemStream.Position = 0
    'Utility.WriteMemoryStreamToDisk(outputMemStream, fileRoot & "\" & zipName) => This line creates a valid zip file on disk, but I need to avoid it.

    Response.AddHeader("Content-Length", outputMemStream.Length)
    Response.Write(outputMemStream) => Writes the file ok in downloads, but apparently corrupted.

    Return zipName

End Function

DownloadFilesFromList:

代码语言:javascript
复制
Public Shared Function DownloadFilesFromList(pdfPathList As List(Of String), fileRoot As String) As List(Of String)

    Dim pdfPathListLocal As New List(Of String)

    Dim Client As WebClient = New WebClient()
    For Each strFile In pdfPathList
        Dim sFile As String = Path.GetFileName(strFile)
        Dim localFile As String = fileRoot + "\" + sFile
        Client.DownloadFile(strFile, localFile)
        pdfPathListLocal.Add(localFile)
    Next

    Return pdfPathListLocal

End Function

GenerateZipOutpuStream:

代码语言:javascript
复制
Public Shared Function GenerateZipOutpuStream(pdfPathListLocal As List(Of String)) As MemoryStream

    Dim outputMemStream = New MemoryStream()
    Dim strmZipOutputStream = New ZipOutputStream(outputMemStream)
    strmZipOutputStream.SetLevel(9)

    Dim objCrc32 As New Crc32()

    For Each strFile In pdfPathListLocal
        Dim strmFile As FileStream = IO.File.OpenRead(strFile)
        Dim abyBuffer(Convert.ToInt32(strmFile.Length - 1)) As Byte
        strmFile.Read(abyBuffer, 0, abyBuffer.Length)

        Dim sFile As String = Path.GetFileName(strFile)
        Dim theEntry As ZipEntry = New ZipEntry(sFile)
        theEntry.DateTime = DateTime.Now
        theEntry.Size = strmFile.Length
        strmFile.Close()

        objCrc32.Reset()
        objCrc32.Update(abyBuffer)
        theEntry.Crc = objCrc32.Value

        strmZipOutputStream.PutNextEntry(theEntry)
        strmZipOutputStream.Write(abyBuffer, 0, abyBuffer.Length)

        'IO.File.Delete(strFile)
    Next

    strmZipOutputStream.Finish()

    Return outputMemStream

End Function

WriteMemoryStreamToDisk:

代码语言:javascript
复制
Public Shared Sub WriteMemoryStreamToDisk(outputMemStream As MemoryStream, file As String)

    Dim buffer As Byte() = outputMemStream.ToArray()
    Dim ms As New MemoryStream(buffer)
    Dim newFile As New FileStream(file, FileMode.Create, FileAccess.Write)
    ms.WriteTo(newFile)
    newFile.Close()
    ms.Close()

End Sub

可能出了什么问题?有什么帮助吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-26 09:42:34

好的,因为我终于找到了我的问题的根源,我会回答自己,我希望能对其他面临同样问题的人有所帮助。

问题在于aspx "UpdatePanel“的工作方式,它执行部分回发,这是不兼容的correctly对象,并阻止它正确工作。

解决方案是禁用引起问题的特定元素的部分回发,在我的例子中:

添加这将导致aDownloadPdfs链接执行完整的回发,并且所有响应对象方法都将按预期工作。

票数 0
EN

Stack Overflow用户

发布于 2021-11-24 13:26:37

在这种情况下,我首先要做的是在文本查看器中打开“损坏”文件,并确定假定(文件类型)文件实际上是否是(文件类型)文件。结果往往证明它不是。例如,当查看常规的ZIP存档时,前2个字符必须是"PK",文件的其余部分是二进制的“胡说八道”。类似于PDF:前几个字符是"%PDF-(PDF版本)“,例如"%PDF-1.7”。

特别是通过下载,您可能会得到一些与您预期不同的东西,例如右击文件链接->“另存为”。事实证明,它不是指向文件的直接链接,而是某种将实际文件推送给客户端的脚本。当您“另存为”该链接时,您得到的是脚本的HTML输出,而不是文件本身。

还有很多反恶意软件,监控网络流量,用其他东西替换可疑文件。

你可能会成为类似的“牺牲品”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70096450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档