3

There is a problem here, the title and description are saving correctly in the SQL database, but it is sending the image file as null. I made some attempts in this way, but I did not get the result I wanted.

How can I print this?

My class:

public class Workplace
{
        [Key]
        [Column("WorkplaceId")]
        public int WorkplaceId { get; set; }

        [NotMapped]
        [Column("WorkplaceThumbnailImage")]
        public byte[]? WorkpaceThumbnailImage { get; set; }

        [Column("WorkplaceTitle")]
        public string? WorkplaceTite { get; set; }

        [Column("WorkplaceExpanation")]
        public string? WorkplaceExplanation { get; set; }
}

Controller post method:

WorkplaceManager workplaceManager = new WorkplaceManager(new EfWorkplaceRepository());

[AllowAnonymous]
[HttpPost]
public IActionResult Index(Workplace p)
{
    Workplace workPlace = new Workplace();
    MemoryStream memoryStream = new MemoryStream();

    foreach (var file in Request.Form.Files)
    {
        p.WorkplaceTite = file.FileName;
        file.CopyTo(memoryStream);

        p.WorkpaceThumbnailImage = memoryStream.ToArray();
    }

    p.WorkpaceThumbnailImage = p.WorkpaceThumbnailImage;
    p.WorkplaceTite = p.WorkplaceTite;
    p.WorkplaceExplanation = p.WorkplaceExplanation;

    workplaceManager.AddWorkplace(p);

    _logger.LogWarning("Success");

    return RedirectToAction("Index", "Home");                    
}

HTML:

@model EntityLayer.Concrete.Workplace;
@using (Html.BeginForm("Workplaces", "Home", FormMethod.Post, new { enctype = "multipart / form - data"}))
{
    <div class="form-group col-md-12">
                        <label for="inputText">Add</label>
                        <input type="File" class="form-control" id="file" name="file1" multiple="multiple"
                               asp-controller="Home" @Html.DisplayFor(model => model.WorkpaceThumbnailImage) method="post"/>
                    </div>
}
13
  • We need to see the markup code for the View or Page that calls the controller method. Commented Sep 11, 2022 at 15:22
  • @Steve I updated the question, I think this is the piece of code you mentioned Commented Sep 11, 2022 at 15:32
  • 1
    i know this is not the solution you are looking for.. but the foreach loop that you have written will overwrite the property values and will always have the last file data irrespective of the number of files you receive. Commented Sep 11, 2022 at 15:35
  • To have a Html.BeginForm know that it should post a file you need to add enctype = ‘multipart/form-data’ attribute Commented Sep 11, 2022 at 15:37
  • 1
    remove the spaces from this: "multipart / form - data" inside the form tag Commented Sep 20, 2022 at 13:09

2 Answers 2

1

My class:

public class Workplace
    {
            [Key]
            [Column("WorkplaceId")]
            public int WorkplaceId { get; set; }
    
            [NotMapped]
            [Column("WorkplaceThumbnailImage")]
            public byte[]? WorkpaceThumbnailImage { get; set; }
    
            [Column("WorkplaceTitle")]
            public string? WorkplaceTite { get; set; }
    
            [Column("WorkplaceExpanation")]
            public string? WorkplaceExplanation { get; set; }
    }

Controler post method:

WorkplaceManager workplaceManager = new WorkplaceManager(new EfWorkplaceRepository());
[AllowAnonymous]
    [HttpPost]
    public async Task<IActionResult> Index(Workplace p)
    {

        Workplace workPlace = new Workplace();
        MemoryStream memoryStream = new MemoryStream();

        foreach (IFormFile file in Request.Form.Files)
        {
            workPlace.WorkplaceTite = file.Name;
            file.CopyToAsync(memoryStream);

            p.WorkplaceThumbnailImage = memoryStream.ToArray();


            workPlace.WorkplaceThumbnailImage = workPlace.WorkplaceThumbnailImage;
            p.WorkplaceTite = p.WorkplaceTite;
            p.WorkplaceExplanation = p.WorkplaceExplanation;
        }
        workplaceManager.AddWorkplace(p);
                
        _logger.LogWarning("Success");

        return RedirectToAction("Index", "Home");
    }

Html:

@model EntityLayer.Concrete.Workplace;
@using (Html.BeginForm("Workplaces", "Home", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
    <div class="col-lg-5">
                    <div class="card text-white rounded-3 sm-3">
                        <button class="btn card text-white rounded-3" type="submit" asp-area="" id="create" asp-controller="Home" asp-action="Hairdresser" style="background-color: #5B2480;">Add</button>
                    </div>
                </div>
}

This is how I edited the codes with your suggestions. It works flawlessly. Thanks to those who contributed. I realized that the problem is caused by missing resources and spaces in "BeginForm".

Sign up to request clarification or add additional context in comments.

1 Comment

you should have given the correct answer to @Ergis (marked as correct answer) with a possible comment about the incorrect BeginForm tag
0

...you are using [NotMapped] on the WorkpaceThumbnailImage property, which exludes it from database mapping.
Try removing it.


Edit
Try this other way:

public class Workplace
{
    [Key]
    [Column("WorkplaceId")]
    public int WorkplaceId { get; set; }

    [Column("WorkplaceThumbnailImage")]
    public byte[]? WorkpaceThumbnailImage { get {
        if(this.MyFile is null) 
            return null;
        using var ms = new MemoryStream();
        this.MyFile.CopyTo(ms);
        return ms.ToArray();
    } }

    [NotMapped]
    public IFormFile MyFile { get; set; }

    [Column("WorkplaceTitle")]
    public string? WorkplaceTite { get; set; }

    [Column("WorkplaceExpanation")]
    public string? WorkplaceExplanation { get; set; }
}  

Controller:

WorkplaceManager workplaceManager = new WorkplaceManager(new EfWorkplaceRepository());

[AllowAnonymous]
[HttpPost]
public IActionResult Index([FromForm] Workplace p)
{
    workplaceManager.AddWorkplace(p);

    _logger.LogWarning("Success");

    return RedirectToAction("Index", "Home");                    
}  

View:
(Renamed the id and name attributes to MyFile because it should match the property name of the class)

@model EntityLayer.Concrete.Workplace;
@using (Html.BeginForm("Workplaces", "Index", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="form-group col-md-12">
        <label for="MyFile">Add</label>
        <input type="file" class="form-control" id="MyFile" name="MyFile" />
    </div>
}

10 Comments

Yes thanks for the fix, I removed the [NotMapped] attribute from the property
sorry, the problem persists. I missed these lines while debugging. p.WorkpaceThumbnailImage = memoryStream.ToArray(); p.WorkpaceThumbnailImage = p.WorkpaceThumbnailImage; These lines are returning blank.
You select file(s) and Request.Form.Files is still empty? Is that what you are saying?
yes that's what i wanted to say
Also, can you confirm, what database are you using (SQL or MySQL), and what is the type of the column of WorkpaceThumbnailImage in db?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.