In the past, we must know that in the Web application, we may encounter the provision of file download function requirements, such as I used to do a customer license management system, customer purchase ERP system, usually we will be based on the machine code of the customer's machine to generate a license file, and this license file is time-limited , expires after one year, the customer will need to re-obtain the license after expiration to indicate the continued use of our super Bull B product, and if the customer no longer acquires the license, the customer is no longer using our Super Bull B product.
Later, I think, yes, we can get a Web application, let the customer log in, enter the machine code, automatically generate the license file, and then the customer click on the page to download on the line. Providing download functionality is actually very common, like many software download stations.
However, have we considered that if we write an application that only provides a few features related to HTTP downloads, we don't seem to need to make a Web site on the machine to get a server. Usually this is the case, a small window program can be finished. So, the idea of using a desktop application to provide HTTP downloads is a thought.
In fact, this implementation is not complicated, A HttpListener class is provided under the System.Net namespace, which listens for incoming HTTP requests from the client and then returns a HttpListenerContext object that can be used to process the request through the HttpListenerContext object /responds to related objects.
According to this idea, we can also easily provide the download function, the original and the Web is the same, that is, in response to the request to insert the Content-disposition header, the value of attachment;filename=< file name > method can be implemented.
[CSharp]View PlainCopy
- Private async void Btnlisten_click (object sender, EventArgs e)
- {
- HttpListener listener = new HttpListener ();
- Listener. Prefixes.add ("http://+:80/download/");
- Listener. Start ();
- btnlisten.enabled = false;
- HttpListenerContext context = await listener. Getcontextasync ();
- if (context! = null)
- {
- //Add Content-disposition header
- Context. Response.appendheader ("content-disposition", "attachment;filename=" + webutility.urlencode ( Path.getfilename (Lblfilepath.text));
- Try
- {
- using (FileStream stream = File.openread (lblfilepath.text))
- {
- //Add content specifier
- Context. Response.ContentType = MediaTypeNames.Application.Octet;
- //Content length
- Context. Response.contentlength64 = stream. Length;
- //Postback data to client
- byte[] buffer = new byte[1024];
- int n = stream. Read (buffer, 0, buffer. Length);
- While (n > 0)
- {
- Context. Response.OutputStream.Write (buffer, 0, N);
- n = stream. Read (buffer, 0, buffer. Length);
- }
- Context. Response.close (); //Off
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show (ex. Message);
- }
- }
- Listener. Stop (); //Stop monitoring
- btnlisten.enabled = true;
- }
Encoding filename with Webutility.urlencode when setting filename prevents garbled characters when the file name contains Chinese.
and send the file to the client is much simpler, and ordinary flow operation is no different, The data read from the file is written to the Response.outputstream stream, and after it is sent, the Close method is called to close the Httplistenerresponse object, so the processing is completed and no more resources are required to open it.
The listener address is http://+:80/download/, which means that HttpListener receives requests for all host names on port 80, and adds download/in the subsequent path only to not conflict with the default localhost address of IIS. Because I turned on IIS.
After running, select a file and then click the button to start listening.
Then enter http://localhost/download/in the browser, press ENTER, you can test the download.
Desktop apps can also provide HTTP file downloads