Search code examples
c#sqljsonasp.net-corepostman

ASP.NET Core Web API trying to upload file and store in database: ERROR: 415: Unsupported Media Type


When trying to create an API that can upload files (and other data) to the database, I am always getting stuck. The API has a raw JSON response with data filling different tables for the database.

However, with Postman, I have to upload the file as form-data or binary file (as the same time as I want to send the raw JSON data), but this never seems to work no matter what I try. So here's the method for uploading files in ASP.NET Core (I'm using a view model class for my methods):

if (item.fileuploadresults != null) 
{
    try 
    {
        foreach(FileUploadResult f in item.fileuploadresults) 
        {
            var parameters = new DynamicParameters();
            parameters.Add("file", f.files);
            parameters.Add("@filename", f.filename);
            parameters.Add("@blob", f.FileBlob);

            var filemessage = await _sqlconnection.QueryAsync < int > ($ @ "INSERT INTO [dbo].[OptionalFile] (file, [FileName], [FileBloB])
          VALUES( { f.files }, @filename, @blob); SELECT SCOPE_IDENTITY();", parameters);

            int FileMessageID = filemessage.First();

            // update cross table
            await _sqlconnection.QueryAsync < int > ($ @ "INSERT INTO [dbo].[ClaimCrossOptionalFile]
           (ClaimID, FileID) VALUES({ claimID }, { FileMessageID });");
        }
    }
    catch (Exception ex) 
    {
        int k = 0;
    }
}

The models look like this:

public class Service
{    
    public IList<FileUploadResult> fileuploadresults { get; set; }
}

public class FileUploadResult
{
    public IFormFile files { get; set; } 

    [MaxLength]
    public byte[] FileBlob { get; set; }

    public long Length { get; set; }
    public string filename { get; set; }
}

The raw JSON:

{
   "service":"Scheduled service",
   "RequestTypeId":"2",
   "CurrentKilometerreading":"10043",
   "OptionalMessage":"Message3",
   "fileuploadresults":[
      {
         "files":""
      }
   ],
   "works":[
      {
         "title":"Service1",
         "price":"200",
         "id":"1998"
      },
      {
         "title":"Service2",
         "price":"300",
         "id":"1999"
      },
      {
         "title":"labour 1",
         "chargePerHour":"10",
         "hours":"2",
         "price":"20.00",
         "id":"2000"
      },
      {
         "title":"labour2",
         "chargePerHour":"20",
         "hours":"2",
         "price":"40.00",
         "id":"2001"
      },
      {
         "title":"Part 1",
         "pricePerUnit":"10",
         "quantity":"11",
         "price":"110.00",
         "id":"2002"
      },
      {
         "title":"Part 1",
         "pricePerUnit":"10",
         "quantity":"11",
         "price":"110.00",
         "id":"2003"
      }
   ]
}

Is it impossible to send both raw json data and form-data in the same request? I've tried changing the content-type in postman as well, with no success.

Thankful for anyone who has any idea on how to proceed.


Solution

  • In case anyone comes across this problem as well, I have found a solution, you can look at this post: using jsonData as a key value to send mixed data in asp: Does not update the database with the values.