private async Task CrearFoto(ProductoFoto model, IFileListEntry foto) { var uploadsFolder = Path.Combine(this._hostingEnvironment.WebRootPath, "images", foto.Name); //var uniqueFileName = Guid.NewGuid() + "." + foto.ContentType.Replace("image/", string.Empty, StringComparison.InvariantCulture); //var filePath = Path.Combine(uploadsFolder, foto.Name); //await using var stream = System.IO.File.Create(filePath); //await foto.CopyToAsync(stream).ConfigureAwait(false); var uploadParams = new ImageUploadParams { File = new FileDescription(filePath), }; await stream.DisposeAsync().ConfigureAwait(false); var uploadResult = await this._cloudinary.UploadAsync(uploadParams).ConfigureAwait(false); model.FotoUrl = uploadResult.SecureUrl; model.FotoPublicId = uploadResult.PublicId; if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } }
public async Task <ActionResult <ProductoFoto> > Crear(int productoId, [FromForm] IFileListEntry foto) { //if (!this.ModelState.IsValid) //{ // return this.BadRequest(this.ModelState); //} var fotoCtx = new ProductoFoto { ProductoId = productoId, IsPrincipal = false, CreatedAt = DateTime.Now, }; await this.CrearFoto(fotoCtx, foto).ConfigureAwait(false); await this._context.ProductoFotos.AddAsync(fotoCtx).ConfigureAwait(false); try { await this._context.SaveChangesAsync().ConfigureAwait(false); } catch (DbUpdateConcurrencyException) { return(this.BadRequest("Hubo un error al guardar sus datos.")); } return(this.CreatedAtAction("Listar", new { fotoCtx.ProductoId })); }
private async Task CrearFoto(ProductoFoto model, Stream foto, string nombre) { var uploadParams = new ImageUploadParams { File = new FileDescription(nombre, foto), }; var uploadResult = await this._cloudinary.UploadAsync(uploadParams).ConfigureAwait(false); model.FotoUrl = uploadResult.SecureUrl; model.FotoPublicId = uploadResult.PublicId; }