async Task CheckName(ActionExecutingContext context) { var boundary = HeaderUtilities.RemoveQuotes(MediaTypeHeaderValue.Parse(context.HttpContext.Request.ContentType).Boundary).Value; try { _reader = new MultipartReader(boundary, context.HttpContext.Request.Body); _section = await _reader.ReadNextSectionAsync(); _printerName = await _section.ReadAsStringAsync(); } catch (Exception e) { context.ActionArguments["requestResult"] = true; } Use_Install_Printers_Api api = new Use_Install_Printers_Api(); var tempCol = await api.GetPrinters(); var tempPrinterName = tempCol.Select(x => x.PrinterName == _printerName).ToList(); if (tempPrinterName.Contains(true)) { _reader = null; _section = null; context.ActionArguments["requestResult"] = true; } }
public async Task <IActionResult> UploadFile() { HttpContext.Features.Get <IHttpResponseBodyFeature>().DisableBuffering(); await GetSections(); if (!await api.CheckName(_tempSectionsCol["PrinterName"])) { if (CheckExtention()) { if (_tempSectionsCol["NetPrinter"].Contains("on")) { _tempSectionsCol["NetPrinter"] = "true"; } else { _tempSectionsCol["NetPrinter"] = "false"; } var uploadFile = _section.AsFileSection(); var formDataContent = new MultipartFormDataContent(); try { formDataContent.Add(new StringContent(_tempSectionsCol["PrinterName"]), "PrinterName"); formDataContent.Add(new StringContent(_tempSectionsCol["NetPrinter"]), "NetPrinter"); formDataContent.Add(new StreamContent(uploadFile.FileStream), "uploadFile"); } catch (Exception e) { log.LogError($"ошибка {e.Message}\n"); ViewBag.message = $"Ошибка - {e.Message}"; } log.LogWarning($"Длина формы {formDataContent.Count()}"); api = new Use_Install_Printers_Api(formDataContent); var result = await api.UploadNewDriverRequest(); if (result.StatusCode == HttpStatusCode.OK) { ViewBag.message = $"Файл {uploadFile.FileName} успешно загружен."; } else { ViewBag.message = $"Ошибка загрузки файла {uploadFile.FileName}."; } log.LogWarning($"{result.StatusCode}\n"); } else { ViewBag.message = $"Ошибка. Архив имел неверный формат."; } } else { ViewBag.message = $"Ошибка. Такой принтер уже есть в базе данных."; } return(View("UploadPrinterDriver", await api.GetPrinters())); }
public UploadPrinterDriverController(ILogger <UploadPrinterDriverController> logger) { api = new Use_Install_Printers_Api(); log = logger; }