public int?AddFotoLeilao(FotoForCreationDto fotoForCreationDto, Cloudinary cloudinary) { var file = fotoForCreationDto.File; var uploadResult = new ImageUploadResult(); if (file.Length > 0) { using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams { File = new FileDescription(file.Name, stream) }; uploadResult = cloudinary.Upload(uploadParams); } } fotoForCreationDto.Url = uploadResult.Uri.ToString(); fotoForCreationDto.PublicId = uploadResult.PublicId; var foto = _mapper.Map <Foto>(fotoForCreationDto); foto.IsMain = true; _fotoRepository.Add(foto); return(foto.Id); }
public HttpResponseMessage UploadFotos(int postId) { var files = HttpContext.Request.Form.Files; foreach (var Image in files) { if (Image != null && Image.Length > 0) { var file = Image; var folderName = Path.Combine("Resources", "Images"); var uploadpath = Path.Combine(Directory.GetCurrentDirectory(), folderName); if (file.Length > 0) { var fileName = file.FileName; using (var fileStream = new FileStream(Path.Combine(uploadpath, fileName), FileMode.Create)) { file.CopyTo(fileStream); fileStream.Close(); var foto = new Foto(fileName); _fotoRepository.Add(foto); } } } } _fotoRepository.SaveChanges(); return(new HttpResponseMessage(HttpStatusCode.Created)); }
public async Task <HttpResponseMessage> UploadPhoto(int fotoSerieID) { decimal bedrag = new Random().Next(10, 30); // Evt. nog bedragen POSTen, voor nu faken met een random bedrag //int fotoSerieID = Convert.ToInt32(Request.GetRouteData().Values["fotoSerieID"]); if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } try { var provider = new MultipartFormDataStreamProvider(filerepo.TempPath); // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); // This illustrates how to get the file names. foreach (MultipartFileData file in provider.FileData) { string md5 = FileFotoRepository.CalculateMD5Hash(File.ReadAllBytes(file.LocalFileName)); int id = repository.Add(fotoSerieID, md5, bedrag); if (id != null && id > 0) // id > 0 dus het plaatje is toegevoegd aan de database { string originalFilename = file.Headers.ContentDisposition.FileName.ToString().ToLower(); originalFilename = FileFotoRepository.RemoveBadPathChars(originalFilename); string[] pointParts = originalFilename.Split('.'); //CommonUtils.GetFileExtension(originalFilename); // Path.GetExtension(originalFilename);//info.Extension; // string extension = "." + pointParts.Last(); string fotoPath = filerepo.Add(file.LocalFileName, fotoSerieID, id, extension); return(Request.CreateResponse(HttpStatusCode.Created)); } else // Niet kunnen toevoegen aan de database, dus plaatje bestond al, dus temp file verwijderen { File.Delete(file.LocalFileName); return(Request.CreateResponse(HttpStatusCode.Conflict)); } } return(Request.CreateResponse(HttpStatusCode.BadRequest)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } }
public ICommandResult Execute(CreateOrUpdateFotoCommand command) { Foto _Foto = AutoMapper.Mapper.Map <CreateOrUpdateFotoCommand, Foto>(command); if (command.Id == 0) { FotoRepository.Add(_Foto); } else { FotoRepository.Update(_Foto); } unitOfWork.Commit(); AutoMapper.Mapper.Map <Foto, CreateOrUpdateFotoCommand>(_Foto, command); return(new CommandResult(true)); }