Original: http: //www.c-sharpcorner.com/Code/2004/Nov/patchdownload.asp Author: Sergey S Translation: Road also flat address: http: //blog.9cbs.net/luyiping/archive/2004/ 11/20 / 188198.aspx
Potentially modify file download in ASP.NET
Source code: PatchDownload.zip Introduction We often need to provide our customers with download links, this link must allow each customer to download related files based on how they are entered on the previous step or other registration information. The webpage has the following interface: In the first web page, the user must enter the username, and in the next web page, we provide links to this user to download the application replica. Once the user downloads the application and starts it, he will see the Welcome window with his name, this window is specified by the first web page. There are many ways to achieve such functions. A method is to modify or recompile the downloadable application / packages using username information transferring to the guest interim end of the server. This task can be implemented in a simple step:
Load the downloadable file to memory. Replace the specified number of bytes of the specified location with a new value. Combine and send a modified file data response to the client.
Let us continue to browse every step. Custom Download Procedure To implement custom action for downloadable resources, we can use buttons or link button controls, which allow you to implement server-side code for the controls of the control. The entire process has two steps: combined with the network response stream and provides this response to a correct HTTP header. The server response stream indicates the file data to be sent to the network client. In order to provide information such as file name and MIME content type transmitted to this network client, we must insert this information into the field of the HTTP header as a response. The following code demonstrates how to load files on the server make the generated file stream and save it to the HTTP response stream.
private void lnkDownload_Click (object sender, System.EventArgs e) {FileStream stream = new FileStream (Server.MapPath ( "TestDownload.exe"), FileMode.Open, FileAccess.Read, FileShare.Read); try {int bufSize = (int ) stream.length; byte [] buf = new byte [bufsize]; int BytesRead = stream.read (buf, 0, bufsize); Response.OutputStream.write (buf, 0, bytesread); response.end ();} finally {stream.close ();
}} According to RFC 2616 and RFC 1806, we need to point out the Content-Type and Content-Disposition file header fields to transmit binary data by the following information. Response.contentType = "Application / OCTET-stream"; response.appendheader ("content-disposition", "attachment; filename =" "testdownload.exe"); write this before writing data into the HTTP response stream Segment code. Modifying the file determines that binary data needs to be modified. Some difficulties. If you have an ordinary executable application file in a fixed location, you can include an executable resource or in a random location containing code. Most of this depends on the tasks you have to complete and can make changes according to different downloadable files. Other solutions are to use parameters to initialize batch files and use custom parameters to recompile your applications or packages. Suppose we discover the correct location in the file and need to replace the original content with new data entered: private void patchdata (byte [] buf, string username, int position) {byte [] patch = encoding.uitode.getbytes (username); System.Array.copy (Patch, 0, BUF, Position, Patch.length);} We simultaneously assume that the file is not very large, and can be loaded into a single memory buffer. Because the downloadable executable may often be recompiled and replaced, the position of the pads also changes. So don't handle these parameters in the ASP.NET DLL code but put them in a web.config file that will be very wise.