public async Task <ActionResult> Post([FromForm] FileUploadApi objFile) { try { if (objFile.files.Length > 0) { if (!Directory.Exists(_environment.WebRootPath + "/Uploads/")) { Directory.CreateDirectory(_environment.WebRootPath + "/Uploads/"); } var host = "http://" + _httpContextAccessor.HttpContext.Request.Host.Value; await using var fileStream = System.IO.File.Create(_environment.WebRootPath + "/Uploads/" + objFile.files.FileName); await objFile.files.CopyToAsync(fileStream); fileStream.Flush(); return(Ok(new UploadResult { path = host + StaticFiles.path + "/Uploads/" + objFile.files.FileName })); } else { return(StatusCode(HttpConstants.NotExtended, new ErrorModel { message = Messages.DefaultMessage })); } } catch (Exception ex) { return(StatusCode(HttpConstants.NotExtended, ex)); } }
public void CesarCifrado2(FileUploadApi objFile, string contra, string nombre) { string archivo = nombre; string clave = contra; string[] FileName1 = objFile.Files.FileName.Split("."); CesarMetodos.CesarAlgoritmo(_environment.WebRootPath + "\\UploadCesar\\" + objFile.Files.FileName, _environment.WebRootPath + "\\UploadCesar\\" + archivo + ".txt", clave); }
public void ZigZagCifrado2(FileUploadApi objFile, int contra, string nombre) { string archivo = nombre; int clave = contra; string[] FileName1 = objFile.Files.FileName.Split("."); ZigZagMetodos.ZigZagAlgortimo(_environment.WebRootPath + "\\UploadZigZag\\" + objFile.Files.FileName, _environment.WebRootPath + "\\UploadZigZag\\" + archivo + ".txt", clave); }
public void EspiralDescifrado(FileUploadApi objFile, int contra, string nombre) { string archivo = nombre; int clave = contra; string[] FileName1 = objFile.Files.FileName.Split("."); RutaMetodos.EspiralAlgortimo2(_environment.WebRootPath + "\\UploadEspiral\\" + objFile.Files.FileName, _environment.WebRootPath + "\\UploadEspiral\\" + archivo + ".txt", clave); }
public async Task <IActionResult> UploadFileText([FromForm] FileUploadApi objFile, [FromForm] FileUploadApi key, [FromForm] FileUploadApi nombreobj) { try { if (objFile.Files.Length > 0) { int i; string nombre = nombreobj.Nombre; //string clave = key.Niveles; int num = Convert.ToInt32(key.Niveles); bool resultado = false; if (int.TryParse(key.Niveles, out i)) { if (i < 10000 && i > 0) { if (!Directory.Exists(_environment.WebRootPath + "\\UploadZigZag\\")) { Directory.CreateDirectory(_environment.WebRootPath + "\\UploadZigZag\\"); } using var _fileStream = System.IO.File.Create(_environment.WebRootPath + "\\UploadZigZag\\" + objFile.Files.FileName); objFile.Files.CopyTo(_fileStream); _fileStream.Flush(); _fileStream.Close(); resultado = true; ZigZagCifrado2(objFile, num, nombre); } else { return(StatusCode(406, "La contraseña está fuera del rango")); } } else { return(StatusCode(406, "La contraseña debe consistir de números")); } var memory = new MemoryStream(); using (var stream = new FileStream(_environment.WebRootPath + "\\UploadZigZag\\" + nombre + ".txt", FileMode.Open)) { await stream.CopyToAsync(memory); } memory.Position = 0; return(File(memory, System.Net.Mime.MediaTypeNames.Application.Octet, nombre + ".txt")); } else { return(null); } } catch { return(null); } }
public async Task <IActionResult> UploadFileText([FromForm] FileUploadApi objFile, [FromForm] FileUploadApi key, [FromForm] FileUploadApi nombreobj) { try { if (objFile.Files.Length > 0) { string nombre = nombreobj.Nombre; string clave = key.Clave; bool resultado = false; if (clave.All(char.IsLetter)) { if (clave.Distinct().Count() == clave.Length) { if (!Directory.Exists(_environment.WebRootPath + "\\UploadCesar\\")) { Directory.CreateDirectory(_environment.WebRootPath + "\\UploadCesar\\"); } using var _fileStream = System.IO.File.Create(_environment.WebRootPath + "\\UploadCesar\\" + objFile.Files.FileName); objFile.Files.CopyTo(_fileStream); _fileStream.Flush(); _fileStream.Close(); resultado = true; CesarCifrado2(objFile, clave, nombre); } else { return(StatusCode(406, "No se puede usar esa clave, porfavor elige una palabra con letras diferentes sin repetir.")); } } else { return(StatusCode(406, "No se puede usar esa clave, porfavor escribe una palabra sin numeros, espacios o caracateres especiales")); } var memory = new MemoryStream(); using (var stream = new FileStream(_environment.WebRootPath + "\\UploadCesar\\" + nombre + ".txt", FileMode.Open)) { await stream.CopyToAsync(memory); } memory.Position = 0; return(File(memory, System.Net.Mime.MediaTypeNames.Application.Octet, nombre + ".txt")); } else { return(null); } } catch { return(null); } }
public async Task <IActionResult> Post() { try { var fileUpload = HttpContext.Request.Form.Files["files"]; Product product = new Product(); product.Name = HttpContext.Request.Form["name"]; product.PriceProduct = int.Parse(HttpContext.Request.Form["priceProduct"]); product.Unit = HttpContext.Request.Form["unit"]; product.TypeId = int.Parse(HttpContext.Request.Form["typeId"]); product.Company = HttpContext.Request.Form["company"]; product.UrlImage = Guid.NewGuid().ToString() + "_" + fileUpload.FileName; FileUploadApi fileUploadApi = new FileUploadApi(); fileUploadApi.files = fileUpload; if (fileUploadApi.files.Length > 0) { var uniqueFileName = product.UrlImage; var uploads = Path.Combine(_enviroment.WebRootPath, "Upload"); var filePath = Path.Combine(uploads, uniqueFileName); fileUploadApi.files.CopyTo(new FileStream(filePath, FileMode.Create)); //to do : Save uniqueFileName to your db table //if (!Directory.Exists(_enviroment.WebRootPath + $"\\Upload\\")) //{ // Directory.CreateDirectory(_enviroment.WebRootPath + $"\\Upload\\"); //} //using (FileStream fileStream = System.IO.File.Create(_enviroment.WebRootPath + $"\\Upload\\" + product.UrlImage)) //{ // fileUploadApi.files.CopyTo(fileStream); // fileStream.Flush(); _context.Products.Add(product); await _context.SaveChangesAsync(); return(CreatedAtAction("Created", product)); //} } else { return(Ok(null)); } }catch (Exception ex) { return(Ok(ex)); } }
public async Task <IActionResult> PutProduct(int id) { var productCheck = _context.Products.Find(id); var fileUpload = HttpContext.Request.Form.Files["files"]; Product product = new Product(); product.Name = HttpContext.Request.Form["name"]; product.ID = int.Parse(HttpContext.Request.Form["id"]); product.Unit = HttpContext.Request.Form["unit"]; product.PriceProduct = int.Parse(HttpContext.Request.Form["priceProduct"]); product.TypeId = int.Parse(HttpContext.Request.Form["typeId"]); product.Company = HttpContext.Request.Form["company"]; product.UrlImage = HttpContext.Request.Form["urlImage"]; product.Price = productCheck.Price; if ("https://localhost:5001/Upload/" + productCheck.UrlImage != product.UrlImage) { FileUploadApi fileUploadApi = new FileUploadApi(); fileUploadApi.files = fileUpload; product.UrlImage = Guid.NewGuid().ToString() + "_" + fileUpload.FileName; if (fileUploadApi.files.Length > 0) { var uniqueFileName = product.UrlImage; var uploads = Path.Combine(_enviroment.WebRootPath, "Upload"); var filePath = Path.Combine(uploads, uniqueFileName); fileUploadApi.files.CopyTo(new FileStream(filePath, FileMode.Create)); } } else { product.UrlImage = productCheck.UrlImage; } Product oldProduct = _context.Products.Find(id); if (oldProduct.Price == 0) { oldProduct.Price = oldProduct.ID; } if (oldProduct.IsDelete) { string sql = "SELECT * FROM Products Where Products.IsDelete = 0 And Products.Price = " + oldProduct.Price; using (SqlConnection connection = new SqlConnection(Connection)) { oldProduct = connection.Query <Product>(sql).LastOrDefault(); } if (oldProduct.UrlImage != product.UrlImage || oldProduct.Name != product.Name || oldProduct.PriceProduct != product.PriceProduct || oldProduct.TypeId != product.TypeId || oldProduct.Company != product.Company) { string sql1 = "SELECT * FROM Products Where Products.IsDelete = 0 And Products.Price = " + oldProduct.Price; using (SqlConnection connection = new SqlConnection(Connection)) { oldProduct = connection.Query <Product>(sql1).LastOrDefault(); } return(Ok(oldProduct)); } else { // Product newProduct = new Product(); // newProduct.Name = product.Name; // newProduct.Type = _context.Types.Find(product.TypeId); // newProduct.PriceProduct = product.PriceProduct; // newProduct.Company = product.Company; // newProduct.Price = oldProduct.Price; // newProduct.UrlImage = oldProduct.UrlImage; // oldProduct.IsDelete = true; // _context.Entry(oldProduct).State = EntityState.Modified; // _context.Products.Add(newProduct); // await _context.SaveChangesAsync(); return(Ok(null)); } } if (product.UrlImage != oldProduct.UrlImage || product.Name != oldProduct.Name || product.PriceProduct != oldProduct.PriceProduct || product.Company != oldProduct.Company || product.TypeId != oldProduct.TypeId) { oldProduct.IsDelete = true; _context.Entry(oldProduct).State = EntityState.Modified; Product productSave = new Product(); productSave.Name = product.Name; productSave.Type = _context.Types.Find(product.TypeId); productSave.PriceProduct = product.PriceProduct; productSave.Company = product.Company; productSave.UrlImage = product.UrlImage; if (oldProduct.Price == 0) { productSave.Price = product.ID; } else { productSave.Price = oldProduct.Price; } _context.Products.Add(productSave); if (id != product.ID) { return(BadRequest()); } try { await _context.SaveChangesAsync(); List <EnterProduct> enterProducts = FindEnterProduct(product.ID); for (int i = 0; i < enterProducts.Count(); i++) { enterProducts[i].Product = productSave; enterProducts[i].ProductId = productSave.ID; _context.Entry(enterProducts[i]).State = EntityState.Modified; } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } } return(Ok(null)); }