Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
Lesson49
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
2
Issues
2
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Пасько Виталий
Lesson49
Commits
55a0af92
Commit
55a0af92
authored
Jun 29, 2022
by
TTrueBenji
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Добавить загрузку файла.
*** Добавил тестовый функционал загрузки файла на сервер. ***
parent
042a5e29
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
93 additions
and
15 deletions
+93
-15
PhonesController.cs
PhoneStore/Controllers/PhonesController.cs
+43
-7
PhoneStore.csproj
PhoneStore/PhoneStore.csproj
+0
-1
UploadService.cs
PhoneStore/Services/UploadService.cs
+15
-0
Startup.cs
PhoneStore/Startup.cs
+1
-2
FileViewModel.cs
PhoneStore/ViewModels/FileViewModel.cs
+9
-0
Index.cshtml
PhoneStore/Views/Phones/Index.cshtml
+3
-5
UploadFile.cshtml
PhoneStore/Views/Phones/UploadFile.cshtml
+22
-0
No files found.
PhoneStore/Controllers/PhonesController.cs
View file @
55a0af92
using
System.Collections.Generic
;
using
System
;
using
System.IO
;
using
System.Linq
;
using
System.Linq
;
using
System.Threading.Tasks
;
using
Microsoft.AspNetCore.Mvc
;
using
Microsoft.AspNetCore.Mvc
;
using
Microsoft.
AspNetCore.Mvc.RazorPages
;
using
Microsoft.
Extensions.Hosting
;
using
PhoneStore.Models
;
using
PhoneStore.Models
;
using
PhoneStore.Services
;
using
PhoneStore.ViewModels
;
namespace
PhoneStore.Controllers
namespace
PhoneStore.Controllers
{
{
public
class
PhonesController
:
Controller
public
class
PhonesController
:
Controller
{
{
private
readonly
MobileContext
_db
;
private
readonly
MobileContext
_db
;
private
readonly
IHostEnvironment
_environment
;
private
readonly
UploadService
_uploadService
;
public
PhonesController
(
MobileContext
db
)
public
PhonesController
(
MobileContext
db
,
IHostEnvironment
environment
,
UploadService
uploadService
)
{
{
_db
=
db
;
_db
=
db
;
_environment
=
environment
;
_uploadService
=
uploadService
;
}
}
/// <summary>
/// <summary>
/// Действие, которое возвращает страницу со списком смартфонов
/// Действие, которое возвращает страницу со списком смартфонов
/// </summary>
/// </summary>
/// <returns></returns>
/// <returns></returns>
[
HttpGet
]
[
HttpGet
]
public
IActionResult
Index
()
public
IActionResult
Index
(
int
?
id
,
string
?
name
)
{
{
List
<
Phone
>
phones
=
_db
.
Phones
.
ToList
();
var
phones
=
_db
.
Phones
.
AsQueryable
();
if
(
id
.
HasValue
)
{
phones
=
_db
.
Phones
.
Where
(
p
=>
p
.
Id
==
id
);
}
if
(!
string
.
IsNullOrEmpty
(
name
))
{
phones
=
phones
.
Where
(
p
=>
string
.
Equals
(
p
.
Name
,
name
,
StringComparison
.
CurrentCultureIgnoreCase
));
}
return
View
(
phones
);
var
phoneList
=
phones
.
ToList
();
return
View
(
phones
.
ToList
());
}
}
[
HttpGet
]
[
HttpGet
]
...
@@ -86,5 +105,22 @@ namespace PhoneStore.Controllers
...
@@ -86,5 +105,22 @@ namespace PhoneStore.Controllers
_db
.
SaveChanges
();
_db
.
SaveChanges
();
return
RedirectToAction
(
"Index"
);
return
RedirectToAction
(
"Index"
);
}
}
[
HttpGet
]
public
IActionResult
UploadFile
()
{
return
View
();
}
[
HttpPost
]
public
async
Task
<
IActionResult
>
UploadFile
(
FileViewModel
model
)
{
string
dirPath
=
Path
.
Combine
(
_environment
.
ContentRootPath
,
"wwwroot/Files"
);
string
fileName
=
$"
{
model
.
File
.
FileName
}
"
;
await
_uploadService
.
UploadAsync
(
dirPath
,
fileName
,
model
.
File
);
return
Ok
(
"Файл успешно загружен"
);
}
}
}
}
}
\ No newline at end of file
PhoneStore/PhoneStore.csproj
View file @
55a0af92
...
@@ -17,7 +17,6 @@
...
@@ -17,7 +17,6 @@
</ItemGroup>
</ItemGroup>
<ItemGroup>
<ItemGroup>
<Folder Include="Phones" />
<Folder Include="Migrations" />
<Folder Include="Migrations" />
</ItemGroup>
</ItemGroup>
...
...
PhoneStore/Services/UploadService.cs
0 → 100644
View file @
55a0af92
using
System.IO
;
using
System.Threading.Tasks
;
using
Microsoft.AspNetCore.Http
;
namespace
PhoneStore.Services
{
public
class
UploadService
{
public
async
Task
UploadAsync
(
string
dirPath
,
string
fileName
,
IFormFile
file
)
{
await
using
var
stream
=
new
FileStream
(
Path
.
Combine
(
dirPath
,
fileName
),
FileMode
.
Create
);
await
file
.
CopyToAsync
(
stream
);
}
}
}
\ No newline at end of file
PhoneStore/Startup.cs
View file @
55a0af92
...
@@ -20,16 +20,15 @@ namespace PhoneStore
...
@@ -20,16 +20,15 @@ namespace PhoneStore
}
}
public
IConfiguration
Configuration
{
get
;
}
public
IConfiguration
Configuration
{
get
;
}
private
IServiceCollection
_services
;
// This method gets called by the runtime. Use this method to add services to the container.
// This method gets called by the runtime. Use this method to add services to the container.
public
void
ConfigureServices
(
IServiceCollection
services
)
public
void
ConfigureServices
(
IServiceCollection
services
)
{
{
_services
=
services
;
string
connection
=
Configuration
.
GetConnectionString
(
"DefaultConnection"
);
string
connection
=
Configuration
.
GetConnectionString
(
"DefaultConnection"
);
services
.
AddDbContext
<
MobileContext
>(
options
=>
options
.
UseSqlite
(
connection
));
services
.
AddDbContext
<
MobileContext
>(
options
=>
options
.
UseSqlite
(
connection
));
services
.
AddControllersWithViews
();
services
.
AddControllersWithViews
();
services
.
AddScoped
<
IBasketService
,
BasketService
>();
services
.
AddScoped
<
IBasketService
,
BasketService
>();
services
.
AddTransient
<
UploadService
>();
}
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
...
...
PhoneStore/ViewModels/FileViewModel.cs
0 → 100644
View file @
55a0af92
using
Microsoft.AspNetCore.Http
;
namespace
PhoneStore.ViewModels
{
public
class
FileViewModel
{
public
IFormFile
File
{
get
;
set
;
}
}
}
\ No newline at end of file
PhoneStore/Views/Phones/Index.cshtml
View file @
55a0af92
...
@@ -66,9 +66,7 @@ else
...
@@ -66,9 +66,7 @@ else
</tr>
</tr>
}
}
</table>
</table>
<div class="d-flex justify-content-end my-5">
<a class="btn btn-outline-info" asp-action="Create" asp-controller="Phones">Добавить устройство</a>
</div>
}
}
<div class="d-flex justify-content-end my-5">
<a class="btn btn-outline-info" asp-action="Create" asp-controller="Phones">Добавить устройство</a>
</div>
PhoneStore/Views/Phones/UploadFile.cshtml
0 → 100644
View file @
55a0af92
@model FileViewModel
@{
ViewBag.Title = "Загрузка файла";
Layout = "_Layout";
}
<h2>Загрузите файл на сервер</h2>
<form asp-action="UploadFile" asp-controller="Phones" method="post" enctype="multipart/form-data">
<fieldset>
<div class="mb-3">
<div class="form-check">
<input class="form-check-input" type="file" asp-for="File">
<label class="form-check-label" asp-for="File">
Добавьте файл
</label>
</div>
</div>
<button type="submit" class="btn btn-outline-info">Добавить</button>
</fieldset>
</form>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment