Asp. NET five step package download Zip file instance _ Practical Tips

Source: Internet
Author: User
Tags http request httpcontext

First of all, share some exciting news:

1. Google has announced its accession. NET Foundation

2. Microsoft joined the Linux Foundation and continued to be good with Linux. The CEO is different.

3. Microsoft Release vs for mac!

First Step Download DLL

Install the following DLL in the NuGet.

Step Two Download method

Project structure

Add the following method to our general handler

///<summary>///bulk pack download Author: Wu Di///</summary>///<param name= "FileName" > file name </param>///<param name= "FilePath" > File virtual path </param> public static void Downloadfil Es (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 = "Bulk Download" + filenames[0] + "et";
   HttpContext.Current.Response.AddHeader ("Content-disposition", "attachment;filename=" + SS + ". zip"); HttpContext.Current.ResponsE.binarywrite (buffer);
   HttpContext.Current.Response.Flush ();
  HttpContext.Current.Response.End (); }

The third step calls the method

Try downloading the files in our 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 Fourth Test results

Run the project!

Here is the package result:

The final discussion of the fifth step
I've seen a lot of friends and co-workers who don't understand how to download files directly to the browser.

In fact, my last remark was wrong. The file is not downloaded to the browser, but along with the HTTP request, you request a series of information such as parameters to flow to the downstream server. And your HTTP server, after receiving the request, after processing, and then to the downstream client, return a series of response results.

All you have to do is read your file into the response stream, and when the browser gets your head notice, it will naturally be downloaded to you as a file.

All your transmissions are due to the HTTP protocol, which is based on the TCP/IP protocol family, and in the HTTP application layer, you make a great contribution to the transport layer, the network layer, and the data link layer. In the transport layer, there is TCP to provide you with secure and reliable transmission support, and all network traffic requires almost all network layer IP protocol, and finally to the data Link layer a series of your visible routing devices.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.