Exemplo n.º 1
0
		public async Task<IActionResult> PostWithImage(ItemEditRequest request)
		{
			//http://damienbod.com/2015/12/05/asp-net-5-mvc-6-file-upload-with-ms-sql-server-filetable/

			Item item;

			if (request.Id == null)
			{
				item = new Item();
				_dbContext.Items.Add(item);
			}
			else
			{
				item = await _dbContext
					.Items
					.FirstOrDefaultAsync(x => x.Id == request.Id);
			}

			if (item == null)
			{
				return HttpNotFound();
			}

			item.Name = request.Name;
			item.FunnyName = request.FunnyName;
			item.ParentId = request.ParentId;

			// read stream
			var file = request.ImageFile;

			if (file != null)
			{
				var fileBytes = new byte[file.Length];
				await file.OpenReadStream().ReadAsync(fileBytes, 0, (int) file.Length);

				// get file name
				var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
				var fileName = parsedContentDisposition.FileName;

				// drop existing file so new file has new id this is important so the url changes and the
				// new image is shown in the UI.
				if (item.File != null)
				{
					_dbContext.Files.Remove(item.File);
				}

				item.File = new File
				{
					Content = fileBytes,
					ContentType = request.ImageFile.ContentType,
					Name = fileName,
					FileType = FileType.Thumbnail
				};
			}


			await _dbContext.SaveChangesAsync();
			return Ok(item.ToNodeModel());
		}
Exemplo n.º 2
0
		public async Task<IActionResult> Post([FromBody] ItemEditRequest request)
		{
			Item item;

			if (request.Id == null)
			{
				item = new Item();
				_dbContext.Items.Add(item);
			}
			else
			{
				item = await _dbContext
					.Items
					.FirstOrDefaultAsync(x => x.Id == request.Id);
			}

			if (item == null)
			{
				return HttpNotFound();
			}

			item.Name = request.Name;
			item.FunnyName = request.FunnyName;
			item.ParentId = request.ParentId;

			await _dbContext.SaveChangesAsync();
			return Ok(item.ToNodeModel());
		}