PTMagic/Monitor/Pages/_get/DownloadFile.cshtml.cs

48 lines
1.5 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.IO;
using Microsoft.Net.Http.Headers;
2018-05-22 10:11:50 +02:00
using Core.Helper;
namespace Monitor.Pages {
public class DownloadFileModel : _Internal.BasePageModelSecure {
public void OnGet() {
// Initialize Config
base.Init();
2021-02-17 18:41:30 +01:00
// Check we have a log in
if (base.IsLoggedIn(this.HttpContext))
{
InitializeDownload();
}
2018-05-22 10:11:50 +02:00
}
private async void InitializeDownload() {
// Zip the file in an non web accessible folder
2018-05-22 10:11:50 +02:00
string fileName = GetStringParameter("f", "");
string tempFolder = PTMagicMonitorBasePath + System.IO.Path.DirectorySeparatorChar + "tmp" + System.IO.Path.DirectorySeparatorChar;
2018-05-22 10:11:50 +02:00
if (System.IO.File.Exists(PTMagicBasePath + fileName)) {
if (!System.IO.Directory.Exists(tempFolder)) {
System.IO.Directory.CreateDirectory(tempFolder);
2018-05-22 10:11:50 +02:00
}
string sourcefilePath = PTMagicBasePath + fileName;
string destinationFilePath = tempFolder + fileName + ".zip";
2018-05-22 10:11:50 +02:00
ZIPHelper.CreateZipFile(new ArrayList() { sourcefilePath }, destinationFilePath);
// Write out the file
var data = System.IO.File.ReadAllBytes(destinationFilePath);
Response.ContentType = "application/zip";
Response.Headers[HeaderNames.CacheControl] = "no-cache";
Response.Headers[HeaderNames.ContentDisposition] = String.Format("attachment; filename={0}", fileName);
await Response.BodyWriter.WriteAsync(new Memory<byte>(data));
Response.BodyWriter.Complete();
2018-05-22 10:11:50 +02:00
}
}
}
}