S
S
Saharman2018-03-18 19:41:02
ASP.NET
Saharman, 2018-03-18 19:41:02

How to upload image to MS SQL database?

I am developing a website in Asp.Net Core. There is a field in the database with type image. You need to fill in the data through the Create page (including adding an image), and then display it on another page. When adding an image to the database, the corresponding image field value for this row is Null. How to solve this problem? To upload an image I tried:

<input asp-for="Img" class="form-control" type="file" />
------------------------------------------------------------------
<input type="file" id="file" class="custom-file-input" name="Img">

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
cicatrix, 2018-03-19
@cicatrix

Most likely in the tag

<form enctype="multipart/form-data" ... > ... </form>
must be put.
Or show the code from the controller.

D
Dmitry Kovalsky, 2018-03-19
@dmitryKovalskiy

Between the markup and the base there is an abyss of a very different secret code, which, judging by the question, you propose to guess. What is the problem?
Did you check the code under debugging?
Does the file come in the request handler?
Does it go through all layers of business logic?
What method is trying to save the file to the database? Through a stored procedure call or direct Insert or a call through some ORM layer?
Why don't you use db data type - byte array?
If the answer to the first question is no, then most likely the correct answer was given by cicatrix

A
Alexander Simonov, 2018-03-19
@SimasikS

@{
    ViewBag.Title = "Upload";
}
<h3>Выберите файл для загрузки</h3>
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new {enctype="multipart/form-data"}))
{
    <input type="file" name="upload" /><br>
    <input type="submit" value="Загрузить" />
}

Here is a form for you
Here is a link for you https://metanit.com/sharp/articles/26.php
And it is desirable to store a link to the image in the database, and not the file itself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question