0

Model Class:

public class TestAspDotNetModelClass
{
    public int Id { get; set; }
    public string Name { get; set; }
    public byte[] file { get; set; }
}

API Controller Action:

public class ValuesController: ApiController
{
    [System.Web.Http.HttpPost]
    public HttpResponseMessage PostComplex(TestAspDotNetModelClass update)
    {
        if (update.fileProfile != null)
        {               
            return Request.CreateResponse(HttpStatusCode.OK);
        } 
        else
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
}

File and value post in postman object post in API but API message show this error

1

1 Answer 1

0

You Can handle the situation with two scenario.

  1. You can use HttpContext.Current.Request
  • Not required as you mentioned in model class byte[] property.

Ref. Link 1 : https://www.c-sharpcorner.com/UploadFile/2b481f/uploading-a-file-in-Asp-Net-web-api/

public class ValuesController: ApiController
{
[System.Web.Http.HttpPost]
    public HttpResponseMessage PostComplex(TestAspDotNetModelClass update)
    {
        if (update.fileProfile != null)
        {
           HttpResponseMessage result = null;  
           var httpRequest = HttpContext.Current.Request;  
           if (httpRequest.Files.Count > 0)  
            {  
                var docfiles = new List<string>();  
                foreach (string file in httpRequest.Files)  
                {  
                    var postedFile = httpRequest.Files[file];  
                    var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName);  
                    postedFile.SaveAs(filePath);  
                    docfiles.Add(filePath);  
                }  
                result = Request.CreateResponse(HttpStatusCode.Created, docfiles);  
            }  
            else  
            {  
                result = Request.CreateResponse(HttpStatusCode.BadRequest);  
            }  
            
        } 
        else
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
 }

  1. You can go with same as you mentioned byte[] that is not possible to create json thats why you can convert byte[] to Base64String then it is possible.

Ref. Link 2 : Saving a base64 string as an image into a folder on server using C# and Web Api

Model Class

public class TestAspDotNetModelClass
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Base64Stringfile { get; set; }
}

API Controller Action

public class ValuesController: ApiController
{
[System.Web.Http.HttpPost]
    public HttpResponseMessage PostComplex(TestAspDotNetModelClass update)
    {
        if (update.Base64Stringfile != null)
        {
            // Convert base 64 string to byte[]
            byte[] fileBytes = Convert.FromBase64String(update.Base64Stringfile);
            // Convert your byte[] to file
            return Request.CreateResponse(HttpStatusCode.OK);
        } 
        else
        {
            return Request.CreateResponse(HttpStatusCode.BadRequest);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for replay. if I do not mention the file/byte[] property, then if I try to receive multiple files and model objects, then I can't catch flies. and you know if I used base64String if any way string is missing, then corrupted a file. and also you know my code style don net Core Possible(please if possible in asp.net, help me)

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.