jeudi 28 janvier 2021

Save multipart/form-Data image to database in asp.net web API

I have the following post method to receive id and image multipart/form-Data and save it in sql server database

[Route("api/save_cus")]
[HttpPost]
public async Task<IHttpActionResult> save_cus([FromBody] MultipartFileData CUS_PHOTO,int id)
{
 if (!Request.Content.IsMimeMultipartContent())
            {
               return Ok( "UnsupportedMediaType");
            }
    string root = HttpContext.Current.Server.MapPath("~/App_Data");
 if (Directory.Exists(root) == false) Directory.CreateDirectory(root);

           var provider = new MultipartFormDataStreamProvider(root);

           // Read the form data.
           await Request.Content.ReadAsMultipartAsync(provider);

                            
           // we take the first file here
           CUS_PHOTO = provider.FileData[0];

           // and the associated datas
           int myInteger;
 if (int.TryParse(provider.FormData["MyIntergerData"], out myInteger) == false)
          throw new ArgumentException("myInteger is missing or not valid.");
var fileContent = File.ReadAllBytes(CUS_PHOTO.LocalFileName);
var Customers = db.Customers.where(a=> a.id == id)
Customers.CUS_PHOTO =fileContent ;
db.SaveChangesAsync();
}

and me model look somthing like this

 public class CUSTOMERS
    {
        public int id { get; set; }

        public string CUS_NAME { get; set; }

        public byte[] CUS_PHOTO { get; set; }
}

then when I try to post this with postman I got UnsupportedMediaType can you help me please to know where is the problem and solve it.




Aucun commentaire:

Enregistrer un commentaire