How to place the path of an image with restricted length in the database

Asked

Viewed 34 times

0

Ola, I have a string type variable that should save the images, but it has length 40 and when trying to put the error path. I have a folder in wwwroot where I store the images and want to store in the variable the path in order to use the variable and return me the image corresponding to that user. I have this code in the controller :

 string caminho = Path.Combine(_hostEnviroment.ContentRootPath, "wwwroot\\Fotos_utilizadores");

        string nome_ficheiro = Path.GetFileName(fotografia.FileName);
        string caminho_completo = Path.Combine(caminho, nome_ficheiro);

        FileStream fs = new FileStream(caminho_completo, FileMode.Create);
        fotografia.CopyTo(fs);
        professores.Fotografia = nome_ficheiro;
        fs.Close();

And in the view a form :

<div class="row">
<div class="col-md-4">
    <form asp-action="RegistarProfessor">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Nome" class="control-label"></label>
            <input asp-for="Nome" class="form-control" />
            <span asp-validation-for="Nome" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Email" class="control-label"></label>
            <input asp-for="Email" class="form-control" />
            <span asp-validation-for="Email" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Fotografia" class="control-label"></label>
            <input asp-for="Fotografia" type="file" accept=".gif, .jpg, .bmp" class="form-control" />
            @*<span asp-validation-for="Fotografia" class="text-danger"></span>*@
        </div>
        <div>
            <input type="hidden" name="fotografia" value="0" />
        </div>
        <div class="form-group form-check">
            @*<label class="form-check-label">
            <input class="form-check-input" asp-for="Sexo" /> @Html.DisplayNameFor(model => model.Sexo)
        </label>*@
            <label asp-for="Sexo" class="control-label"></label>
            <select asp-for="Sexo" class="form-control">
                <option value="true">Feminino</option>
                <option value="false">Masculino</option>
            </select>
        </div>
        <div class="form-group">
            <label asp-for="Especialidade" class="control-label"></label>
            <input asp-for="Especialidade" class="form-control" />
            <span asp-validation-for="Especialidade" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Password" class="control-label"></label>
            <input type="password" asp-for="Password" id="password" placeholder="password" class="form-control" />
            <input type="button" id="showPassword" value="Mostrar" class="button" />
            <span asp-validation-for="Password" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Estado" class="control-label"></label>
            <input type="text" value="1" asp-for="Estado" disabled="disabled" class="form-control" />
            <span asp-validation-for="Estado" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </form>

Does anyone know how I can solve this ? In order to save the image path in the database ?

  • What’s the error? What validations are made in your model?

  • It is also not necessary to repeat the question, it is possible to edit your old question.

  • What is the value attributed to teachers. Photography?

  • @tvdias the value assigned to teachers. Photography is the name of the photograph > teachers.Photography = filename;

  • Yes, and what is the value in your tests?

  • for example, cross_fit.jpg

  • And what is the error presented?

  • is not a specific error, just want to save the path to the image there so you can use the corresponding image aqele teacher

  • "when trying to put the error path". did not understand. There is error or there is no?

  • "Sqlexception: String or Binary data would be truncated. The statement has been terminated. " This error appears

  • The error is very clear... In this case it is important to confirm the size of the columns in the database and all past data. Place a breakpoint in the EF save and check exactly all values passed before being written to the database. I believe there’s nothing to be done but that.

  • The problem is that the string has size 40 and is small, it is not possible to keep only the name and extension ?

  • You say that the column has size 40 and is sending "cross_fit.jpg" to the database, so either you are not sending this value, or the column does not have this size or the problem is in another column. Whatever the case, it is impossible to guess. It is necessary to check all the parts.

  • gives 40 with the entire path... but because you simply do not increase the column size in the database?

  • I’m having trouble updating the database

  • @Leandroangelo error that one of the tables already exists in the database

  • Only if you are trying to create the table again, what you need is to change its structure.

  • Yes and I to change it in the package manager console write update-database and this error appears : There is already an Object named 'Aulas_group' in the database.

Show 13 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.