Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a201f8

Browse files
Upload image in asp.net core
1 parent cdf077c commit 9a201f8

16 files changed

+456
-327
lines changed

‎Webgentle.BookStore/Webgentle.BookStore/Controllers/BookController.cs‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IO;
34
using System.Linq;
45
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Hosting;
57
using Microsoft.AspNetCore.Mvc;
68
using Microsoft.AspNetCore.Mvc.Rendering;
79
using Webgentle.BookStore.Models;
@@ -12,12 +14,16 @@ namespace Webgentle.BookStore.Controllers
1214
public class BookController : Controller
1315
{
1416
private readonly BookRepository _bookRepository = null;
15-
private readonly LanguageRepository _languageRepository = null;
16-
17-
public BookController(BookRepository bookRepository, LanguageRepository languageRepository)
17+
private readonly LanguageRepository _languageRepository = null;
18+
private readonly IWebHostEnvironment _webHostEnvironment;
19+
20+
public BookController(BookRepository bookRepository,
21+
LanguageRepository languageRepository,
22+
IWebHostEnvironment webHostEnvironment)
1823
{
1924
_bookRepository = bookRepository;
20-
_languageRepository = languageRepository;
25+
_languageRepository = languageRepository;
26+
_webHostEnvironment = webHostEnvironment;
2127
}
2228

2329
public async Task<ViewResult> GetAllBooks()
@@ -56,6 +62,18 @@ public async Task<IActionResult> AddNewBook(BookModel bookModel)
5662
{
5763
if (ModelState.IsValid)
5864
{
65+
if (bookModel.CoverPhoto != null)
66+
{
67+
string folder = "books/cover/";
68+
folder += Guid.NewGuid().ToString() + "_" + bookModel.CoverPhoto.FileName;
69+
70+
bookModel.CoverImageUrl = "/"+folder;
71+
72+
string serverFolder = Path.Combine(_webHostEnvironment.WebRootPath, folder);
73+
74+
await bookModel.CoverPhoto.CopyToAsync(new FileStream(serverFolder, FileMode.Create));
75+
}
76+
5977
int id = await _bookRepository.AddNewBook(bookModel);
6078
if (id > 0)
6179
{
Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
6-
namespace Webgentle.BookStore.Data
7-
{
8-
public class Books
9-
{
10-
public int Id { get; set; }
11-
public string Title { get; set; }
12-
public string Author { get; set; }
13-
public string Description { get; set; }
14-
public string Category { get; set; }
15-
public int LanguageId { get; set; }
16-
public int TotalPages { get; set; }
17-
public DateTime? CreatedOn { get; set; }
18-
public DateTime? UpdatedOn { get; set; }
19-
20-
public Language Language { get; set; }
21-
}
22-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Webgentle.BookStore.Data
7+
{
8+
public class Books
9+
{
10+
public int Id { get; set; }
11+
public string Title { get; set; }
12+
public string Author { get; set; }
13+
public string Description { get; set; }
14+
public string Category { get; set; }
15+
public int LanguageId { get; set; }
16+
public int TotalPages { get; set; }
17+
public string CoverImageUrl { get; set; }
18+
public DateTime? CreatedOn { get; set; }
19+
public DateTime? UpdatedOn { get; set; }
20+
21+
public Language Language { get; set; }
22+
}
23+
}

‎Webgentle.BookStore/Webgentle.BookStore/Migrations/20200627134021_addednewcolumn.Designer.cs‎

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
namespace Webgentle.BookStore.Migrations
4+
{
5+
public partial class addednewcolumn : Migration
6+
{
7+
protected override void Up(MigrationBuilder migrationBuilder)
8+
{
9+
migrationBuilder.AddColumn<string>(
10+
name: "CoverImageUrl",
11+
table: "Books",
12+
nullable: true);
13+
}
14+
15+
protected override void Down(MigrationBuilder migrationBuilder)
16+
{
17+
migrationBuilder.DropColumn(
18+
name: "CoverImageUrl",
19+
table: "Books");
20+
}
21+
}
22+
}
Lines changed: 92 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,92 @@
1-
// <auto-generated />
2-
using System;
3-
using Microsoft.EntityFrameworkCore;
4-
using Microsoft.EntityFrameworkCore.Infrastructure;
5-
using Microsoft.EntityFrameworkCore.Metadata;
6-
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
7-
using Webgentle.BookStore.Data;
8-
9-
namespace Webgentle.BookStore.Migrations
10-
{
11-
[DbContext(typeof(BookStoreContext))]
12-
partial class BookStoreContextModelSnapshot : ModelSnapshot
13-
{
14-
protected override void BuildModel(ModelBuilder modelBuilder)
15-
{
16-
#pragma warning disable 612, 618
17-
modelBuilder
18-
.HasAnnotation("ProductVersion", "3.1.4")
19-
.HasAnnotation("Relational:MaxIdentifierLength", 128)
20-
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
21-
22-
modelBuilder.Entity("Webgentle.BookStore.Data.Books", b =>
23-
{
24-
b.Property<int>("Id")
25-
.ValueGeneratedOnAdd()
26-
.HasColumnType("int")
27-
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
28-
29-
b.Property<string>("Author")
30-
.HasColumnType("nvarchar(max)");
31-
32-
b.Property<string>("Category")
33-
.HasColumnType("nvarchar(max)");
34-
35-
b.Property<DateTime?>("CreatedOn")
36-
.HasColumnType("datetime2");
37-
38-
b.Property<string>("Description")
39-
.HasColumnType("nvarchar(max)");
40-
41-
b.Property<int>("LanguageId")
42-
.HasColumnType("int");
43-
44-
b.Property<string>("Title")
45-
.HasColumnType("nvarchar(max)");
46-
47-
b.Property<int>("TotalPages")
48-
.HasColumnType("int");
49-
50-
b.Property<DateTime?>("UpdatedOn")
51-
.HasColumnType("datetime2");
52-
53-
b.HasKey("Id");
54-
55-
b.HasIndex("LanguageId");
56-
57-
b.ToTable("Books");
58-
});
59-
60-
modelBuilder.Entity("Webgentle.BookStore.Data.Language", b =>
61-
{
62-
b.Property<int>("Id")
63-
.ValueGeneratedOnAdd()
64-
.HasColumnType("int")
65-
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
66-
67-
b.Property<string>("Description")
68-
.HasColumnType("nvarchar(max)");
69-
70-
b.Property<string>("Name")
71-
.HasColumnType("nvarchar(max)");
72-
73-
b.HasKey("Id");
74-
75-
b.ToTable("Language");
76-
});
77-
78-
modelBuilder.Entity("Webgentle.BookStore.Data.Books", b =>
79-
{
80-
b.HasOne("Webgentle.BookStore.Data.Language", "Language")
81-
.WithMany("Books")
82-
.HasForeignKey("LanguageId")
83-
.OnDelete(DeleteBehavior.Cascade)
84-
.IsRequired();
85-
});
86-
#pragma warning restore 612, 618
87-
}
88-
}
89-
}
1+
// <auto-generated />
2+
using System;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Infrastructure;
5+
using Microsoft.EntityFrameworkCore.Metadata;
6+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
7+
using Webgentle.BookStore.Data;
8+
9+
namespace Webgentle.BookStore.Migrations
10+
{
11+
[DbContext(typeof(BookStoreContext))]
12+
partial class BookStoreContextModelSnapshot : ModelSnapshot
13+
{
14+
protected override void BuildModel(ModelBuilder modelBuilder)
15+
{
16+
#pragma warning disable 612, 618
17+
modelBuilder
18+
.HasAnnotation("ProductVersion", "3.1.4")
19+
.HasAnnotation("Relational:MaxIdentifierLength", 128)
20+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
21+
22+
modelBuilder.Entity("Webgentle.BookStore.Data.Books", b =>
23+
{
24+
b.Property<int>("Id")
25+
.ValueGeneratedOnAdd()
26+
.HasColumnType("int")
27+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
28+
29+
b.Property<string>("Author")
30+
.HasColumnType("nvarchar(max)");
31+
32+
b.Property<string>("Category")
33+
.HasColumnType("nvarchar(max)");
34+
35+
b.Property<string>("CoverImageUrl")
36+
.HasColumnType("nvarchar(max)");
37+
38+
b.Property<DateTime?>("CreatedOn")
39+
.HasColumnType("datetime2");
40+
41+
b.Property<string>("Description")
42+
.HasColumnType("nvarchar(max)");
43+
44+
b.Property<int>("LanguageId")
45+
.HasColumnType("int");
46+
47+
b.Property<string>("Title")
48+
.HasColumnType("nvarchar(max)");
49+
50+
b.Property<int>("TotalPages")
51+
.HasColumnType("int");
52+
53+
b.Property<DateTime?>("UpdatedOn")
54+
.HasColumnType("datetime2");
55+
56+
b.HasKey("Id");
57+
58+
b.HasIndex("LanguageId");
59+
60+
b.ToTable("Books");
61+
});
62+
63+
modelBuilder.Entity("Webgentle.BookStore.Data.Language", b =>
64+
{
65+
b.Property<int>("Id")
66+
.ValueGeneratedOnAdd()
67+
.HasColumnType("int")
68+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
69+
70+
b.Property<string>("Description")
71+
.HasColumnType("nvarchar(max)");
72+
73+
b.Property<string>("Name")
74+
.HasColumnType("nvarchar(max)");
75+
76+
b.HasKey("Id");
77+
78+
b.ToTable("Language");
79+
});
80+
81+
modelBuilder.Entity("Webgentle.BookStore.Data.Books", b =>
82+
{
83+
b.HasOne("Webgentle.BookStore.Data.Language", "Language")
84+
.WithMany("Books")
85+
.HasForeignKey("LanguageId")
86+
.OnDelete(DeleteBehavior.Cascade)
87+
.IsRequired();
88+
});
89+
#pragma warning restore 612, 618
90+
}
91+
}
92+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /