Код: Выделить всё
[HttpGet]
[JWTAuthentication]
public HttpResponseMessage DownloadFiles()
{
logger.LogTraceStart();
string folderPath = HttpContext.Current.Server.MapPath("~/XSLT");
string zipPath = HttpContext.Current.Server.MapPath("~/XSLTlol.zip");
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
try
{
// Create the ZIP file
if (Directory.Exists(folderPath))
{
// Ensure the directory and files are accessible and not locked
ZipFile.CreateFromDirectory(folderPath, zipPath, CompressionLevel.Optimal, true);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Directory not found.");
}
// Verify the ZIP file was created
if (!File.Exists(zipPath))
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Failed to create ZIP file.");
}
// Create the HTTP response
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(new FileStream(zipPath, FileMode.Open, FileAccess.Read));
// result.Content.AddHea
//result.AddHeader("content-disposition", "attachment; filename=" + "_Templates.Zip");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "XSLTlol"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
catch (Exception ex)
{
logger.LogError(ex);
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "An error occurred while creating the ZIP file.");
}
finally
{
this.session.Flush();
logger.LogTraceEnd();
}
}
Подробнее здесь: https://stackoverflow.com/questions/787 ... f-a-folder
Мобильная версия