【问题标题】:Download stream to file下载流到文件
【发布时间】:2020-03-13 15:36:02
【问题描述】:

我有一个提供流的服务。此流应写入 pdf 文件。

我试过这个方法,但是没用:

Using hcwHandler As IHealthCareWorkerServiceHandler = Container.CurrentContainer.Resolve(Of IHealthCareWorkerServiceHandler)()
  stream = hcwHandler.DownloadPrescription(New DownloadPrescriptionRequest With {
    .ProfessionCode = ucSelectProfession.ProfessionCode,
    .RizivNumber = ucSelectProfession.Nihdi,
    .Culture = language
  }).Result
End Using

Dim buffer As Byte() = Encoding.ASCII.GetBytes(stream.ToString())

Using ms As New MemoryStream(buffer)
  'write to file
  Using file As New FileStream("prescription.pdf", FileMode.Create, FileAccess.Write)
    ms.WriteTo(file)
  End Using
End Using

我也尝试了其他几种解决方案,但似乎都没有奏效。 我从来没有得到文件。

谁能帮帮我?

【问题讨论】:

  • Dim file As New FileStream("prescription.pdf", FileMode.Create, FileAccess.Write)这一行放一个断点以确保到达。
  • @preciousbetine 到达该行。所以这不是问题。
  • 该文件应该在您当前的文件夹中。你在哪里检查文件?
  • @preciousbetine 我找不到 pdf 文件
  • @BartSchelkens 1) 如您所知,如果 PDF 的二进制数据作为字符串处理(例如使用stream.ToString()),那么它将被损坏,Encoding.ASCII.GetBytes 甚至会损坏它更多的。 2) 始终提供文件的完整路径,以便您知道它在哪里。

标签: asp.net vb.net pdf stream


【解决方案1】:

您可以在没有 MemoryStream 的情况下读取 Using 块中的缓冲区(如果那里有数据)(如下面的代码所示),并放置一个完整的绝对路径,如 C:\Documents\PdfFolder\prescription.pdf

Using saveFileDialog1 As New SaveFileDialog()
  saveFileDialog1.InitialDirectory = "C:\Documents"
  saveFileDialog1.RestoreDirectory = True
  saveFileDialog1.Filter = "PDF files|*.pdf" ' change here for csv or xls
  saveFileDialog1.FileName = "yourDefaultfileName.pdf"

  If saveFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
    If Len(saveFileDialog1.FileName.Length) > 0 Then
      Try
        Dim pdfPath As String = System.IO.Path.GetFullPath(saveFileDialog1.FileName)

        Using file As New FileStream(pdfPath, FileMode.Create, FileAccess.Write)
          file.Write(buffer, 0, buffer.Length)
        End Using

        MessageBox.Show("Saved in " & pdfPath)

      Catch
        MsgBox("Not a valid name file.")

      End Try
    End If
  End If
End Using

【讨论】:

  • 通常当您在浏览器中下载某些内容时,您可以在浏览器底部看到(在 Google Chrome 中)。我怎么能用我正在写的文件做到这一点?我现在可以找到该文件,所以它正在编写中。
  • 记住@Andrew Morton 关于文件损坏的说法。您需要直接/以另一种方式处理流
  • @G3nt_M3caj 也许您也可以将 C# 代码从 How do I save a stream to a file in C#? 转换为 VB.NET 以供 OP 使用,以获得完整的答案;)
  • 是的,完成了。我希望 OP 在这种情况下理解“yourStreamHere”必须是 Stream(她的流)而不是路径:)。
【解决方案2】:

这里是How do I save a stream to a file in C#的VB.NET版本

Using fromWebFileStream As Stream = New StreamReader(yourStreamHere)
  Using localFileStream As FileStream = File.Create("C:\Documents\PathTo\File")
    'if you aren't at the begin of your stream uncomment this
    'fromWebFileStream.Seek(0, SeekOrigin.Begin)
    fromWebFileStream.CopyTo(localFileStream)
  End Using
End Using

【讨论】:

  • 我得到了异常:fromWebFileStream.Seek 上的“指定的方法不受支持”
  • 如果您在流的开头,您可以删除 Seek 方法。我更新了代码,抱歉我分心了:)
猜你喜欢
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
相关资源
最近更新 更多