I have to transfer a file object from php to c# webapi. Somehow, in c#, the file contents are coming as Null. This is the partial PHP code after taking out the other stuff
<?php
$fileContents = file_get_contents($_FILES['fileToUpload']['tmp_name']);
$uploadFile = new UploadFile(); //custom object
$uploadFile->fileName = "test"; //string
$uploadFile->fileContent = $fileContents; //string
$data_string=json_encode($uploadFile);
$url="http://localhost:62672/api/uploadfile";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_VERBOSE, TRUE);
//curl_setopt($ch, CURLOPT_PORT, 801);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
));
?>
And here is the c# controller code & model code
public class CrmUploadFileModel
{
public string fileName { get; set; }
public byte[] fileContent { get; set; }
}
public class UploadFileController : BaseController
{
public IHttpActionResult Post([FromBody]CrmUploadFileModel value)
{
// here .. i find that the value.fileContent is null.
}
Just started learning .NET and hence dont know if anything is wrong in the .NET code.
Using the same PHP code, i am able to send succesfuly the file contents to a .NET webservice, which is a separate application.
Thanks in advance. Aman