V
V
Victoria Kabishova2020-12-20 14:26:50
ASP.NET
Victoria Kabishova, 2020-12-20 14:26:50

How to save uploaded pictures using ASP.NET Core 5.0?

Hello, I am doing the final project, the theme of the project is the site of crowdfunding companies. And on the page for creating a company there is a field for adding pictures. Pictures must be uploaded using Drag and Drop. When creating the code, I relied on this site - https://habr.com/en/post/125424/ . I did everything except for this: "The server part. All that remains for us is to write a simple handler that will save the file in the location we need. I will not go into writing a handler much, but just give a small example in PHP."

<?php
$uploaddir = getcwd().DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR;
$uploadfile = $uploaddir.basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
?>


Please tell me how to save pictures using ASP.NET Core 5.0. I can't use PHP language since I don't use it in my project.

Here is the code for the company creation page:
@page
@model MyWebSIte.Pages.Company.CreateModel

@{
    ViewData["Title"] = "Create";
}

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer

<link rel="stylesheet" href="/css/create.css">

<div class="container-fluid">
    <div class="container">
        <div class="row">
            <div class="col">
                <h1>@Localizer["Создание"]</h1>

                <h4>@Localizer["Компании"]</h4>
                <hr />
                <div class="row">
                    <div class="col-md-4">
                        <form method="post">
                            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                            <div class="form-group">
                                <label asp-for="Company.Title" class="control-label"></label>
                                <input asp-for="Company.Title" class="form-control" [email protected]["Название"] />
                                <span asp-validation-for="Company.Title" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                <label asp-for="Company.ReleaseDate" class="control-label"></label>
                                <input asp-for="Company.ReleaseDate" class="form-control" />
                                <span asp-validation-for="Company.ReleaseDate" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                <label asp-for="Company.Rating" class="control-label"></label>
                                <input asp-for="Company.Rating" class="form-control" [email protected]["Рейтинг"] />
                                <span asp-validation-for="Company.Rating" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                <label asp-for="Company.Bonus" class="control-label"></label>
                                <input asp-for="Company.Bonus" class="form-control" [email protected]["Бонус"] />
                                <span asp-validation-for="Company.Bonus" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                <label asp-for="Company.Description" class="control-label"></label>
                                <input asp-for="Company.Description" class="form-control" [email protected]["Описание"] />
                                <span asp-validation-for="Company.Description" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                <label asp-for="Company.Thematics" class="control-label"></label>
                                <input asp-for="Company.Thematics" class="form-control" [email protected]["Тематика"] />
                                <span asp-validation-for="Company.Thematics" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                @*<label asp-for="Company.Images" class="control-label"></label>
                                    <input asp-for="Company.Images" class="form-control" placeholder="Изобраения" />
                                    <span asp-validation-for="Company.Images" class="text-danger"></span>*@

                                <div id="dropZone">
                                    Для загрузки, перетащите файл сюда.
                                </div>
                            </div>
                            <div class="form-group">
                                <label asp-for="Company.Video" class="control-label"></label>
                                <input asp-for="Company.Video" class="form-control" [email protected]["Видео"] />
                                <span asp-validation-for="Company.Video" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                <label asp-for="Company.Topic" class="control-label"></label>
                                <input asp-for="Company.Topic" class="form-control" [email protected]["Тема"] />
                                <span asp-validation-for="Company.Topic" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                <label asp-for="Company.News" class="control-label"></label>
                                <input asp-for="Company.News" class="form-control" [email protected]["Новости"] />
                                <span asp-validation-for="Company.News" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                <label asp-for="Company.Price" class="control-label"></label>
                                <input asp-for="Company.Price" class="form-control" [email protected]["Цена"] />
                                <span asp-validation-for="Company.Price" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                <label asp-for="Company.Tags" class="control-label"></label>
                                <input asp-for="Company.Tags" class="form-control" [email protected]["Тэги"] />
                                <span asp-validation-for="Company.Tags" class="text-danger"></span>
                            </div>
                            <div class="form-group">
                                <input type="submit" value="Create" class="btn btn-primary" />
                            </div>
                        </form>
                    </div>
                </div>

                <div>
                    <a asp-page="Index">@Localizer["Вернуться к списку"]</a>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="/js/create.js"></script>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}


Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2020-12-20
@Parsifal31017

The very first line in Google: https://metanit.com/sharp/aspnet5/21.3.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question