AP.NET WEBAPI: Compress a CSV file and Send the Response as ZIP
WEB API: Compress a CSV file and Send the Response as ZIP
In ASP.NET WEB API, we can create an API call for sending the file in response. Which means that the user can call an URL and gets the required file as a download.
Zipping a file in earlier versions of dotnet framework 4.0 was quite lengthy (sometimes it has needed to use third part tools). From 4.5 on wards, we have an assembly for that task specifically with one single line of code.
C# code for the API:
We need to add reference of System.IO.Compression.FileSystem to the project and use the below using statement to make the ZIP statement work.
using System.IO.Compression;
Once the steps are done, need to map the directory to store the generated CSV file temporarily. Once mapping is done, write all text content the the file. Need to provide the final zip file name along its path (fully qualified file name).
Zip.CreateFromDirectory is the important line to ZIP. It just takes the source folder contents, zips it and saves it at destination folder.
Once file is zipped, we send the response as file stream by adding required headers for the download to work.
Happy Coding......
[Route("api/Dwonload/DownloadZip")]
[HttpGet]
public HttpResponseMessage DownloadZip(string filename)
{
//Get file content from your service laywer
string fileDetails = _downloadService.GetCSV();
//path to store the file temporarily
string folder = System.Web.HttpContext.Current.Server.MapPath("~/Content/");
if (!(Directory.Exists(folder+"Files/")))
Directory.CreateDirectory(folder+"Files/");
string sourcepath = System.Web.HttpContext.Current.Server.MapPath("~/Content/Files/");
string zippath = System.Web.HttpContext.Current.Server.MapPath("~/Content/"+filename+".zip");
System.IO.File.WriteAllText(sourcepath + filename+ ".csv", fileDetails);
if (File.Exists(zippath))
File.Delete(zippath);
ZipFile.CreateFromDirectory(sourcepath, zippath);
var stream = new FileStream(zippath, FileMode.Open);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); //attachment will force download
result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(zippath);
// Delete the CSV files after respose
Array.ForEach(Directory.GetFiles(sourcepath), File.Delete);
return result;
}
In ASP.NET WEB API, we can create an API call for sending the file in response. Which means that the user can call an URL and gets the required file as a download.
Zipping a file in earlier versions of dotnet framework 4.0 was quite lengthy (sometimes it has needed to use third part tools). From 4.5 on wards, we have an assembly for that task specifically with one single line of code.
C# code for the API:
We need to add reference of System.IO.Compression.FileSystem to the project and use the below using statement to make the ZIP statement work.
using System.IO.Compression;
Once the steps are done, need to map the directory to store the generated CSV file temporarily. Once mapping is done, write all text content the the file. Need to provide the final zip file name along its path (fully qualified file name).
Zip.CreateFromDirectory is the important line to ZIP. It just takes the source folder contents, zips it and saves it at destination folder.
Once file is zipped, we send the response as file stream by adding required headers for the download to work.
Happy Coding......
[Route("api/Dwonload/DownloadZip")]
[HttpGet]
public HttpResponseMessage DownloadZip(string filename)
{
//Get file content from your service laywer
string fileDetails = _downloadService.GetCSV();
//path to store the file temporarily
string folder = System.Web.HttpContext.Current.Server.MapPath("~/Content/");
if (!(Directory.Exists(folder+"Files/")))
Directory.CreateDirectory(folder+"Files/");
string sourcepath = System.Web.HttpContext.Current.Server.MapPath("~/Content/Files/");
string zippath = System.Web.HttpContext.Current.Server.MapPath("~/Content/"+filename+".zip");
System.IO.File.WriteAllText(sourcepath + filename+ ".csv", fileDetails);
if (File.Exists(zippath))
File.Delete(zippath);
ZipFile.CreateFromDirectory(sourcepath, zippath);
var stream = new FileStream(zippath, FileMode.Open);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); //attachment will force download
result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(zippath);
// Delete the CSV files after respose
Array.ForEach(Directory.GetFiles(sourcepath), File.Delete);
return result;
}
Thanks for sharing valuable information. Your blogs were helpful to Dot NET learners. I request to update the blog through step-by-step. Also, find the Dot net news at Dot NET Online Training Hyderabad
ReplyDelete