ASP. NET five-step package download Zip file instance, asp. netzip

Source: Internet
Author: User

ASP. NET five-step package download Zip file instance, asp. netzip

First, let's share some exciting news:

1. Google has announced its participation in the. NET Foundation.

2. Microsoft joins the Linux Foundation and continues to make good use of Linux. The CEO is different.

3. Microsoft released VS For Mac!

Step 1 download the dll

Install the following dll in Nuget

Step 2 Download Method

Project Structure

Add the following method to our general handler:

/// <Summary> /// batch package and download Author: wu Shuang // </summary> /// <param name = "fileName"> file name </param> /// <param name = "filePath"> Virtual File Path </param> public static void DownLoadFiles (string fileName, string filePath) {List <string> fileNames = fileName. split ('| '). toList (); List <string> filePaths = filePath. split ('| '). toList (); MemoryStream MS = new MemoryStream (); byte [] buffer = null; using (ZipFile file = ZipFile. create (MS) {file. beginUpdate (); file. nameTransform = new MyNameTransfom (); filePaths. forEach (t => {file. add (HttpContext. current. server. mapPath (t);}); file. commitUpdate (); buffer = new byte [ms. length]; ms. position = 0; ms. read (buffer, 0, buffer. length);} string ss = "batch download" + fileNames [0] + ", etc."; HttpContext. current. response. addHeader ("content-disposition", "attachment; filename =" + ss + ". zip "); HttpContext. current. response. binaryWrite (buffer); HttpContext. current. response. flush (); HttpContext. current. response. end ();}

Step 3 call the Method

Download the file in the Pic directory!

 public void ProcessRequest(HttpContext context)  {   //context.Response.ContentType = "text/plain";   //context.Response.Write("Hello World");   DownLoadFiles("xxx", "~/Pic/00.gif|~/Pic/02.png");  }

Step 4 Test Results

Run the project!

The following is the packaging result:

The last part of Step 5
I have seen many friends and colleagues who do not understand how to directly download files to a browser.

In fact, my last sentence is already wrong. The file is not downloaded to the browser, but with the HTTP Request, your Request sends parameters and other information to the downstream server. After receiving the request, your HTTP server processes the request and returns a series of Response results to the downstream client.

What you need to do is to read your file stream into the response stream and download it as a file when the browser receives your header notification.

All your transmission is attributed to the HTTP protocol, which is based on the TCP/IP protocol family. At the HTTP application layer, you have made great contributions to the transport layer, network layer, and data link layer. At the transport layer, TCP provides secure and reliable transmission support for you, and almost all network communication requires the IP protocol at the network layer, and finally a series of visible routing devices at the data link layer.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.