麻烦Response.WriteFile / Response.BinaryWrite / Response.TransmitFile(ASP.NET) [英] Trouble with Response.WriteFile / Response.BinaryWrite / Response.TransmitFile (ASP.NET)

查看:1052
本文介绍了麻烦Response.WriteFile / Response.BinaryWrite / Response.TransmitFile(ASP.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的网页生成,我希望用户能够下载,一旦其创作完成CSV文件。

I've got a simple web-page that generates a CSV file that I want the user to be able to download once its creation is complete.

下面是我的情况的摘要:

Here's a summary of my situation:


        
  1. CSV文件可以在内存或磁盘上创建的。没关系我。

  2.     
  3. 当我做传输CSV文件,我不希望它继续驻留在磁盘上。

  4.     
  5. 我尝试使用Response.WriteFile,.TransmitFile,.BinaryWrite和各种.WRITE code,但无济于事。

  6.     
  7. 当我说的无济于事的,我的意思是,而不是CSV文件传输,我传送CSV文件的内容 PLUS的全部当前页面!

  8.     
  9. 我真的很困惑,因为我有在做实质上是一样的另一页,但捆绑的zip文件的PDF文件,以及它工作得很好。

  10.     
  11. 我真的Windows桌面开发者,但我想成为一名Web开发人员。 (很抱歉,如果这是一个愚蠢或noob问题。)

  1. The CSV file can be created in-memory or on disk. Doesn't matter to me.
  2. Once I'm done transmitting the CSV file, I do not want it to continue residing on disk.
  3. I've tried various code using Response.WriteFile, .TransmitFile, .BinaryWrite and .Write, but to no avail.
  4. When I say "to no avail", I mean that rather than the CSV file being transmitted, I'm transmitting the contents of the CSV file PLUS THE ENTIRETY OF THE CURRENT PAGE!
  5. I'm really confused since I have another page that's doing essentially the same thing, but with PDF files bundled in a zip file, and it works just fine.
  6. I'm really a Windows desktop developer, but I'm trying to become a web developer. (So sorry if this is a stupid or noob question.)

这里的code我有工作派的.zip包含PDF文件:

Here's the code I've got working to send the .zip containing PDF files:

Protected Sub SaveReportsButton_Click(sender As Object, e As EventArgs) Handles SaveReportsButton.Click
    '' The .zip file is created elsewhere
    Dim fullPath As String = Server.MapPath("~/Reports/" & Session.SessionID & ".zip")
    Response.ContentType = "APPLICATION/OCTET-STREAM"
    Dim fileToDownload As New FileInfo(fullPath)
    Dim disHeader As String = "Attachment; Filename=""Reports.zip"""
    Response.AppendHeader("Content-Disposition", disHeader)
    Response.Flush()
    Response.WriteFile(fileToDownload.FullName)
End Sub

这里的code,我试图让工作来传输CSV文件:

Here's the code I'm trying to get working to transmit the CSV file:

Protected Sub saveButton_Click(sender As Object, e As System.EventArgs) Handles SaveOutput.Click
    Dim list As List(Of String) = {"000000000", "000000000", "000000000"}.ToList()
    Dim sb As New Text.StringBuilder
    For i As Integer = 0 To list.Count - 1
        Dim str As String = list(i)
        sb.Append(str)
        If i < list.Count - 1 Then
            sb.Append(",")
        End If
    Next
    Dim dirPath As String = Server.MapPath("~/Output/" & Session.SessionID)
    IO.Directory.CreateDirectory(dirPath)
    Dim filePath As String = Server.MapPath("~/Output/" & Session.SessionID & "/Output.csv")
    IO.File.WriteAllText(filePath, sb.ToString())
    Response.ContentType = "text/csv"
    Dim disHeader As String = "Attachment; Filename=""Output.csv"""
    Response.AppendHeader("Content-Disposition", disHeader)
    Response.Flush()
    Response.WriteFile(filePath)
End Sub

就像我说的,我希望限制本页面创建并留在服务器上的文件,所以我想摆脱CSV文件一次,我用它做。

Like I said, I wish to limit the files created and left on the server by this page, so I'd like to get rid of the CSV file once I'm done with it.

任何帮助吗?

谢谢!

推荐答案

用的Response.Write和到Response.End试试这个

try this with Response.Write and Response.End

Protected Sub saveButton_Click(sender As Object, e As System.EventArgs) Handles SaveOutput.Click
    Dim list As List(Of String) = {"000000000", "000000000", "000000000"}.ToList()
    Dim sb As New Text.StringBuilder
    For i As Integer = 0 To list.Count - 1
        Dim str As String = list(i)
        sb.Append(str)
        If i < list.Count - 1 Then
            sb.Append(",")
        End If
    Next
    Response.Clear()
    Response.ContentType = "text/csv"
    Dim disHeader As String = "Attachment; Filename=""Output.csv"""
    Response.AppendHeader("Content-Disposition", disHeader)
    Response.Flush()
    Response.Write(sb.ToString())
    Response.End()
End Sub

这篇关于麻烦Response.WriteFile / Response.BinaryWrite / Response.TransmitFile(ASP.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆