Asp.net download memorystream
This article will show you how to buffer data into a MemoryStream from a query and output the buffered data back to the browser as a text file. There are many occasions which require web developers to provide downloadable content to end-users in the form of text files, Excel files, etc.
This is pretty straight forward if the files already exist and are physically located somewhere the user can access via the browser. However, this requirement becomes a little more complicated if the data that comprises the file is dynamically generated based on user input.
An example of this is an application that allows a user to select the number of widgets their company produced between DateA and DateB and then have that data immediately downloaded as a text file. In this situation the text file has to be created on the fly and sent to the browser. Meeting the requirement is pretty straightforward. The following example shows you how to query a SQL Server database, buffer the output to a MemoryStream, and then have that data transferred to the browser as a Tab-Delimited text file.
The first step in this example is to query the data out of the database and put it into a MemoryStream. The following function give shows how this can be done. IsDBNull counter? GetValue counter. Flush ; sw. Close ; sc. One way to overcome this is to use a HTTP Handler to execute the code that generates the text file and sends it back to the browser. This handler implements three methods, two of which are required by the IhttpHandler interface and one which is used to retirieve data from the database and return it as a memory stream.
Web; using System. IO; using System. SqlClient; using System. IsDBNull counter? GetValue counter. Flush ; sw. Close ; sc. Flush ; mstream. This lets the browser know how much data is being transfered context. Begins an asynchronous read operation. Begins an asynchronous write operation. Closes the current stream and releases any resources such as sockets and file handles associated with the current stream.
Instead of calling this method, ensure that the stream is properly disposed. Reads the bytes from the current memory stream and writes them to another stream, using a specified buffer size. Reads the bytes from the current stream and writes them to another stream, using a specified buffer size. Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified cancellation token.
Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size. Asynchronously reads all the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token. Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token.
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. Allocates a WaitHandle object. Releases all resources used by the Stream. Releases the unmanaged resources used by the MemoryStream class and optionally releases the managed resources. Releases the unmanaged resources used by the Stream and optionally releases the managed resources. Asynchronously releases the unmanaged resources used by the Stream. Waits for the pending asynchronous read to complete.
Ends an asynchronous write operation. Overrides the Flush method so that no action is performed. Asynchronously clears all buffers for this stream and causes any buffered data to be written to the underlying device. Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests. Retrieves the current lifetime service object that controls the lifetime policy for this instance.
Gets the Type of the current instance. Creates a shallow copy of the current Object. Creates a shallow copy of the current MarshalByRefObject object. This API supports the product infrastructure and is not intended to be used directly from your code.
Provides support for a Contract. Reads a sequence of bytes from the current memory stream and advances the position within the memory stream by the number of bytes read. When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests. Asynchronously reads a sequence of bytes from the current memory stream, writes the sequence into destination , advances the position within the memory stream by the number of bytes read, and monitors cancellation requests.
Writes the stream contents to a byte array, regardless of the Position property. Returns the array of unsigned bytes from which this stream was created.
The return value indicates whether the conversion succeeded. Writes the sequence of bytes contained in source into the current memory stream and advances the current position within this memory stream by the number of bytes written. When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests. Asynchronously writes the sequence of bytes contained in source into the current memory stream, advances the current position within this memory stream by the number of bytes written, and monitors cancellation requests.
0コメント