I'm trying to save some data to my database and uploading an image at the same time using a Web Api.
I failed to do this because I cannot send multipart/from and JSON at the same time. I got this error:
request entity's media type 'multipart/form-data' is not supported for this resource.
This is my code:
public IHttpActionResult Post([FromBody]Post userpost)
{
Upload upload = new Upload();
int postid = Convert.ToInt32(postDB.AddPost(userpost));
var filename = postid + "_"+Guid.NewGuid().ToString();
upload.UploadFile(filename, Request);
return Ok(HttpStatusCode.Accepted);
}
public class Upload:IUpload
{
private const string Container = "images";
public async Task<bool> UploadFile(string filename, HttpRequestMessage Request)
{
var accountName = ConfigurationManager.AppSettings["storage:account:name"];
var accountKey = ConfigurationManager.AppSettings["storage:account:key"];
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer imagesContainer = blobClient.GetContainerReference(Container);
var provider = new AzureImages(imagesContainer,filename);
try
{
await Request.Content.ReadAsMultipartAsync(provider);
}
catch (Exception ex)
{
return false;
}
if (string.IsNullOrEmpty(filename))
{
return false;
}
return true;
}
}
}
I'm testing it using Postman Thanks in advance.