Best way to save stream to file in asp.net 3.5?

I have a Stream object that is populated with the contents of the XSD file, which I have as an embedded resource in the project I'm working on:

using ( Stream xsdStream = assembly.GetManifestResourceStream( xsdFile ) )
{
  // Save the contents of the xsdStream here...
}

In this usage block, I would like to ask the user to save the file on the Internet, where they can choose to save this XSD file contained in the stream.

What is the best way to do this? I’m completely lost and I can’t provide Google with the correct conditions for receiving an appropriate response.

Thanks!

+3
source share
4 answers

AJAX, Response.WriteFile. MemoryStream. . , VB.NET . , -, .. .

Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.Net
Imports System.IO

Partial Class DownloadFile
Inherits System.Web.UI.Page

Protected Sub page_load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim url As String = Request.QueryString("DownloadUrl")
If url Is Nothing Or url.Length = 0 Then Exit Sub

'Initialize the input stream
Dim req As HttpWebRequest = WebRequest.Create(url)
Dim resp As HttpWebResponse = req.GetResponse()
Dim bufferSize As Integer = 1 

'Initialize the output stream
Response.Clear()
Response.AppendHeader("Content-Disposition:", "attachment; filename=download.zip")
Response.AppendHeader("Content-Length", resp.ContentLength.ToString)
Response.ContentType = "application/download"

'Populate the output stream
Dim ByteBuffer As Byte() = New Byte(bufferSize) {}
Dim ms As MemoryStream = New MemoryStream(ByteBuffer, True)
Dim rs As Stream = req.GetResponse.GetResponseStream()
Dim bytes() As Byte = New Byte(bufferSize) {}
While rs.Read(ByteBuffer, 0, ByteBuffer.Length) > 0
Response.BinaryWrite(ms.ToArray())
Response.Flush()
End While

'Cleanup
Response.End()
ms.Close()
ms.Dispose()
rs.Dispose()
ByteBuffer = Nothing
End Sub
End Class
+2

:

Response.AddHeader "Content-Disposition","attachment; filename=" & xsdFile

Content-Type text/plain Content-Length . .

+1
private void DownloadEmbeddedResource( 
  string resourceName, Assembly resourceAssembly, string downloadFileName )
{
  using ( Stream stream = resourceAssembly.GetManifestResourceStream( resourceName ) )
  {
    if ( stream != null )
    {
      Response.Clear();
      string headerValue = string.Format( "attachment; filename={0}", downloadFileName );
      Response.AppendHeader( "Content-Disposition:", headerValue );
      Response.AppendHeader( "Content-Length", stream.Length.ToString() );
      Response.ContentType = "text/xml";

      var byteBuffer = new Byte[1];

      using ( var memoryStream = new MemoryStream( byteBuffer, true ) )
      {
        while ( stream.Read( byteBuffer, 0, byteBuffer.Length ) > 0 )
        {
          Response.BinaryWrite( memoryStream.ToArray() );
          Response.Flush();
        }
      }

      Response.End();
    }
  }
}

. , tsilb. JohannesH, , (, ).

, ... , , , , . , ?

!

+1
source

It seems to me that you should take a look at the WebResource.axd handler.

Microsoft has an excellent article on this.

Edit:

It seems that tilb beat my answer for about a minute. However, AssemblyResourceLoader (aka. WebResource.axd) is already implemented to do this for you and to do it right, and don't forget that this puppy supports output caching. So keep using it instead and save yourself the trouble.;)

0
source

Source: https://habr.com/ru/post/1703637/


All Articles