/
agontar
/
TOROS
Обзор
Документация
Войти
/
agontar
/
TOROS
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Webapplication_new/Controllers/ImagesController.cs
159 строк
5 KB
gav
initial upload
17 сен 2024, 16:04
17 сен 2024, 16:04
356ccc9
Код
Авторство
О чём код?
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Web; using System.Diagnostics; using WebApplication1.Models; using WebApplication1.Services; using EasyModbus; using Newtonsoft.Json; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc.Rendering; using System.Net; using System.IO; using System.Xml.Linq; using Quartz.Util; using static System.Net.WebRequestMethods; using Microsoft.Extensions.Hosting.Internal; using System.Text.RegularExpressions; using System; namespace WebApplication1.Controllers { public class ImagesController : Controller { IWebHostEnvironment Environment; private readonly ILogger<HomeController> _logger; public ImagesController(ILogger<HomeController> logger, IWebHostEnvironment environment) { _logger = logger; Environment = environment; var hosting = new HostingEnvironment(); // Environment = hosting as IWebHostEnvironment; } public IActionResult Index(FileModel m) { //Path. //WebImage photo = null; string wwwPath = this.Environment.WebRootPath; string path = Path.Combine(wwwPath, "Uploads"); string[] files = Directory.GetFiles(path); for (int i = 0; i < files.Length; i++) { string file = files[i].Replace(wwwPath+"\\Uploads\\", ""); //Console.WriteLine(file); m.Files.Add(file); ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", file); } return View(m); } [HttpPost] public IActionResult Index(string deleteFile, WriteRead wr, IFormFile monFile, int cost, string listbox) { //Path. Console.WriteLine($"Cost: {cost} {listbox}"); try { if (cost == 1) { string fileName = string.Empty; if (monFile.FileName != null) { fileName = monFile.FileName; fileName = Path.GetFileName(monFile.FileName); } string wwwPath = this.Environment.WebRootPath; string path = Path.Combine(wwwPath, "Uploads", fileName); using (FileStream stream = new FileStream(path, FileMode.Create)) { monFile.CopyTo(stream); //uploadedFiles.Add(fileName); ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName); } Console.WriteLine($"{deleteFile} {fileName} {cost}"); } if (cost == 2) { Console.WriteLine(deleteFile); if (listbox.IsNullOrWhiteSpace()) { Console.WriteLine("Ошибка ввода"); //return View(); return RedirectToAction("Index"); } string wwwPath = this.Environment.WebRootPath; //string path = Path.Combine(wwwPath, "Uploads", deleteFile); string path = Path.Combine(wwwPath, "Uploads", listbox); wr.DeleteFile(path); } return RedirectToAction("Index"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return View(); } } public IActionResult Reports() { string wwwPath = this.Environment.WebRootPath; string path = Path.Combine(wwwPath, "Reports"); string[] filePaths = Directory.GetFiles(path); //Copy File names to Model collection. List<FileModel> files = new List<FileModel>(); foreach (string filePath in filePaths) { files.Add(new FileModel { FileName = Path.GetFileName(filePath) }); } return View(files); //return View(); } public FileResult DownloadFile(string fileName) { //Build the File Path. string wwwPath = this.Environment.WebRootPath; string path = Path.Combine(wwwPath, "Reports", fileName); //Read the File data into Byte Array. byte[] bytes = System.IO.File.ReadAllBytes(path); //Send the File to Download. return File(bytes, "application/octet-stream", fileName); } public IActionResult DeleteFile(string fileName, WriteRead wr) { //Build the File Path. string wwwPath = this.Environment.WebRootPath; string path = Path.Combine(wwwPath, "Reports", fileName); wr.DeleteFile(path); return RedirectToAction("Reports"); } } }