0

I am struggling with uploading a picture inside a folder in asp.net core. I have the following model class:

public  class User
{
    public int IdUser { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string ConfPassword { get; set; }
    public DateTime Created { get; set; } = DateTime.Now;
    public DateTime? Dob { get; set; }
    public byte[] Picture { get; set; }

    public virtual ICollection<Login> Login { get; set; }
}

This is the post method in the API controller:

[HttpPost]
public async Task<string> Post(IFormFile file,User user)
{
    string fName = file.FileName;
    string path = Path.Combine(_environment.ContentRootPath, "uploads/" + fName);
    using (var stream = new MemoryStream())
    {
        using(var fileStream=new FileStream(path, FileMode.Create))
        { 
            await stream.CopyToAsync(fileStream);
            user.Picture = stream.ToArray();
            string s = Convert.ToBase64String(user.Picture);
            return s;
        }
    }
}

I have declared an IFormFile file in the method which I convert to byte array and I add the path where the image has to be saved. Every time I'm trying to post the image as binary in Postman it returns

415 Unsupported Media type

and if I try as a form-data it will return

System.NullReferenceException: Object reference not set to an instance of an object

The client side actually stores the image file as a byte array in the database, but I need to save it in the folder on the server-side. Can someone help me understand why it keeps returning the null exception? What am I doing wrong?

ekad
  • 13,718
  • 26
  • 42
  • 44
AlexC
  • 21
  • 1
  • 6

2 Answers2

0

Make sure the parameter name is file in postman:

enter image description here

And the Content-Type must be multipart/form-data

enter image description here

In addition, your post method should like this:

[HttpPost]
public async Task<string> Post(IFormFile file, User user)
{

    string fName = file.FileName;
    string path = Path.Combine(_environment.ContentRootPath, "uploads/" + fName);
    using (var stream = new MemoryStream())
    {
        await file.CopyToAsync(stream);
        user.Picture = stream.ToArray();
        string s = Convert.ToBase64String(user.Picture);
        return s;
    }
}
mj1313
  • 6,367
  • 1
  • 2
  • 22
  • mj1313 thanks for the solution.Unfortunately it's not working as it has the same behavior as before....in Postman though in the Headers section I don't get the content type as mutipart/form-data when I send the request.Also,there's no way that i can save the image into the specified folder if i don't pass the file path,in this case I just declare the variable,but never use it. – AlexC Jul 30 '20 at 10:27
0

I managed to make it work.I had a problem with my project path as for some reason it took the old path of the project as I have moved it to another solution.This is the method I used in the controller:

  [HttpPost]
    public async Task Post(IFormFile file)
    {
        if (string.IsNullOrWhiteSpace(_environment.WebRootPath))
        {
            _environment.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
        }

        var uploads = Path.Combine(_environment.WebRootPath, "uploads");

        if (!Directory.Exists(uploads)) Directory.CreateDirectory(uploads);

        if (file.Length > 0)
        {
            using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create))
            {
                await file.CopyToAsync(fileStream);
            }
        }

    } 
AlexC
  • 21
  • 1
  • 6