Use a plug-in to compress and download files.

Source: Internet
Author: User

Use a plug-in to compress and download files.

The method used to directly access the Internet:

/// <Summary> /// function: compress the file (only files in the directory at the lower level of the folder are temporarily compressed, and the folder and its sub-level are ignored) /// </summary> /// <param name = "dirPath"> compressed folder path </param> /// <param name = "zipFilePath"> generated path of the compressed file, if it is null, the first-level directory is the same as the compressed folder by default. The name is: folder name. Zip </param> /// <param name = "err"> error message </param> /// <returns> whether compression is successful </returns> public static bool ZipFile (string dirPath, string zipFilePath, out string err) {// if a File with the same name exists on the server, delete if (File. exists (zipFilePath) {try {Fil E. Delete (zipFilePath) ;}catch {}} err = ""; if (dirPath = string. Empty) {err = "the folder to be compressed cannot be blank! "; Return false;} if (! Directory. Exists (dirPath) {err = "the folder to be compressed does not exist! "; Return false;} // when the compressed file name is empty, use the folder name pai.zip if (zipFilePath = string. empty) {if (dirPath. endsWith ("\") {dirPath = dirPath. substring (0, dirPath. length-1);} zipFilePath = dirPath + ". zip ";} try {string [] filenames = Directory. getFiles (dirPath); using (ZipOutputStream s = new ZipOutputStream (File. create (zipFilePath) {s. setLevel (9); byte [] buffer = new byte [4096]; foreach (string file in filenames) {ZipEntry entry = new ZipEntry (Path. getFileName (file); entry. dateTime = DateTime. now; s. putNextEntry (entry); using (FileStream fs = File. openRead (file) {int sourceBytes; do {sourceBytes = fs. read (buffer, 0, buffer. length); s. write (buffer, 0, sourceBytes);} while (sourceBytes> 0);} s. finish (); s. close () ;}} catch (Exception ex) {err = ex. message; return false;} return true ;}

Package and download code directly

String temp_path = HttpContext. current. request. mapPath (". \ query \ "); String path = temp_path + DateTime. now. toString ("yyyyMMddHHmmss"); string newPath = path + "\"; Directory. createDirectory (path); for (int I = 0; I <ds. tables. count; I ++) {DataToCSV. saveCSV (ds. tables [I], newPath);} String result = String. empty; Boolean flag = DataToCSV. zipFile (newPath, temp_path + "MesData.zip", out result); if (flag) {// Delete the generated folder Directory. delete (path, true); // download FileInfo fileInfo = new FileInfo (temp_path + "MesData.zip"); Response. clear (); Response. clearContent (); Response. clearHeaders (); Response. addHeader ("Content-Disposition", "attachment?filename=mesdata.zip"); Response. addHeader ("Content-Length", fileInfo. length. toString (); Response. addHeader ("Content-Transfer-Encoding", "binary"); Response. contentType = "application/octet-stream"; Response. contentEncoding = System. text. encoding. getEncoding ("UTF-8"); Response. writeFile (fileInfo. fullName); Response. flush (); HttpContext. current. applicationInstance. completeRequest (); // delete a copy // File. delete (filePath); Response. close ();} else {// prompt message}

 

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.