HttpResponse.WriteFile Method

Definition

Writes the specified file directly to an HTTP response output stream.

Overloads

WriteFile(IntPtr, Int64, Int64)

Writes the specified file directly to an HTTP response output stream.

WriteFile(String, Int64, Int64)

Writes the specified file directly to an HTTP response output stream.

WriteFile(String)

Writes the contents of the specified file directly to an HTTP response output stream as a file block.

WriteFile(String, Boolean)

Writes the contents of the specified file directly to an HTTP response output stream as a memory block.

WriteFile(IntPtr, Int64, Int64)

Writes the specified file directly to an HTTP response output stream.

public void WriteFile (IntPtr fileHandle, long offset, long size);

Parameters

fileHandle
IntPtr

The file handle of the file to write to the HTTP output stream.

offset
Int64

The byte position in the file where writing will start.

size
Int64

The number of bytes to write to the output stream.

Exceptions

fileHandler is null.

offset is less than 0.

-or-

size is greater than the file size minus offset.

Examples

The following example writes all the contents of a text file named Login.txt (which might contain literal HTML text and input controls) directly to the output stream.

String FileName;
 FileStream MyFileStream;
 IntPtr FileHandle;
 long StartPos = 0, FileSize;

 FileName = "c:\\temp\\Login.txt";

 MyFileStream = new FileStream(FileName, FileMode.Open);
 FileHandle = MyFileStream.Handle;
 FileSize = MyFileStream.Length;

 Response.Write("<b>Login: </b>");
 Response.Write("<input type=text id=user /> ");
 Response.Write("<input type=submit value=Submit /><br><br>");

 Response.WriteFile(FileHandle, StartPos, FileSize);

 MyFileStream.Close();

Remarks

When this method is used with large files, calling the method might throw an exception. The size of the file that can be used with this method depends on the hardware configuration of the Web server.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

WriteFile(String, Int64, Int64)

Writes the specified file directly to an HTTP response output stream.

public void WriteFile (string filename, long offset, long size);

Parameters

filename
String

The name of the file to write to the HTTP output stream.

offset
Int64

The byte position in the file where writing will start.

size
Int64

The number of bytes to write to the output stream.

Exceptions

offset is less than 0.

-or-

size is greater than the file size minus offset.

The filename parameter is null.

Examples

The following example writes all the contents of a text file named Login.txt (which might contain literal text and HTML input controls) directly to the output stream.

String FileName;
 FileInfo MyFileInfo;
 long StartPos = 0, FileSize;

 FileName = "c:\\temp\\login.txt";
 MyFileInfo = new FileInfo(FileName);
 FileSize = MyFileInfo.Length;

 Response.Write("Please Login: <br>");
 Response.WriteFile(FileName, StartPos, FileSize);

Remarks

When this method is used with large files, calling the method might throw an exception. The size of the file that can be used with this method depends on the hardware configuration of the Web server.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

WriteFile(String)

Writes the contents of the specified file directly to an HTTP response output stream as a file block.

public void WriteFile (string filename);

Parameters

filename
String

The name of the file to write to the HTTP output.

Exceptions

The filename parameter is null.

Examples

The following example writes all the contents of a text file named Login.txt (which might contain literal HTML text and input controls) directly to the output stream.

Response.Write("Please Login: <br>");
 Response.WriteFile("login.txt");

Remarks

When this method is used with large files, calling the method might throw an exception. The size of the file that can be used with this method depends on the hardware configuration of the Web server.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

WriteFile(String, Boolean)

Writes the contents of the specified file directly to an HTTP response output stream as a memory block.

public void WriteFile (string filename, bool readIntoMemory);

Parameters

filename
String

The name of the file to write into a memory block.

readIntoMemory
Boolean

Indicates whether the file will be written into a memory block.

Exceptions

The filename parameter is null.

Examples

The following example writes a file to memory.

Response.WriteFile("login.txt", true);

Remarks

When this method is used with large files, calling the method might throw an exception. The size of the file that can be used with this method depends on the hardware configuration of the Web server.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1