Reading a Binary File and Using Response.Binarywrite()

Reading a binary file and using Response.BinaryWrite()

Try adding

Response.End();

after the call to Response.BinaryWrite().

You may inadvertently be sending other content back after Response.BinaryWrite which may confuse the browser. Response.End will ensure that that the browser only gets what you really intend.

Simultaneously read and write binary file

You're opening the file twice - once for reading and once for writing. That means that one FileStream needs FileAccess.Read and FileShare.Write while the other needs FileAccess.Write and FileShare.Read. This code is tested and verified with a file that had already had an Integer and a String written to it with a BinaryWriter:

Dim filePath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Test.bin")

Using source = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Write),
destination = File.Open(filePath, FileMode.Open, FileAccess.Write, FileShare.Read),
reader As New BinaryReader(source),
writer As New BinaryWriter(destination)
Dim n = reader.ReadInt32()

writer.Write(98765)
writer.Write("What's up doc?")

Dim sz = reader.ReadString()
End Using

Note that you should only specify Read or Write if that's all that's needed. Only specify ReadWrite if you know that you will or might need both. The FileAccess value is for what this FileStream will do or may do to the file while the FileShare value is for what other FileStream objects opened on the same file are allowed to do.

response.BinaryWrite problems (Text file contains PDF encoding)

In your admin section:

string AttachmentID = selectedRow.Cells[1].Text;

In your user section:

string AttachmentID = grdAttachments.DataKeys[0].Value.ToString();

I would have to assume that the user section assignment is not doing what you expect it to, since the logic isn't the same for both. Verify what AttachmentID is set to in each case, perhaps via logging.

Send file using Response.BinaryWrite() and delete it afterwards

I found a solution: The extracted files are saved in a special directory and everytime a user runs the document.asp it checks this directory for files older than one hour and deletes them.

I think it's the simplest way to manage, but furthermore I would prefer a solution where the document is deleted after downloading.

How does the binarywrite() method works?

In this case context.Response is is a stream that represents the data that is being sent to the client (most likely the web browser) that issues the http request. Context.Response is being provided to you automatically by the ASP "engine" behind the scenes. Anything you write to that stream will end up being sent out to the browser.

The generic http handler automatically provides you with an HttpContext instance named Context. HttpContext contains two properties called Request and Response that allow you to access the request that your generic handler is serving and the corresponding response. The handler should do any processing that it needs to do and send the result by writing it's reply to the Response stream.

BinaryWrite vs WriteFile

The other consideration is whether this is a file which is written one time or frequently. If you are frequently writing this file, then you might want to cache it, thus Response.BinaryWrite makes the most sense.

If you have it in memory, I would not write it to the file system and use Response.WriteFile.

No response in postback after writing binary to the response

Turns out there was an UpdatePanel in the MasterPage, the problem was solved when I registered the button as a post back control ... That hurts I know.

So I solved it by using this line of code in the Page_Load event:

ScriptManager.GetCurrent(Me).RegisterPostBackControl(btnSubmit)


Related Topics



Leave a reply



Submit