I have tried inserting pdf file into the database, but it is getting inserted as 0 for int datatype and null for varbinary(max) into the database. I have also tested using postman, by passing the data into web API controller, but still it is getting inserted as 0 and null into the database.
[HttpPost]
[ActionName("addReceiptDocument")]
public int insertReceiptDocument(receiptDocument rd)
{
rd = new receiptDocument();
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ERPConnectionString"].ConnectionString;
SqlCommand sqlCmd = new SqlCommand();
//DateTime now = DateTime.Now;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "INSERT_RECEIPT_DOCUMENT";
sqlCmd.Connection = myConnection;
sqlCmd.Parameters.AddWithValue("@RDID", rd.RDID);
sqlCmd.Parameters.AddWithValue("@RECEIPTID", rd.RECEIPTID);
//var request = HttpContext.Current.Request;
//var filePath = "Z:\\Templates\\Images\\" + request.Headers["filename"];
//using (var fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
//{
// request.InputStream.CopyTo(fs);
//}
sqlCmd.Parameters.AddWithValue("@DOCUMENT", rd.DOCUMENT);
myConnection.Open();
int rowInserted = sqlCmd.ExecuteNonQuery();
myConnection.Close();
return 1;
}
$scope.submit = function (files) {
//var fd = new FormData();
//Take the first selected file
//fd.append("payloadFile", 33);
//fd.append("payloadFile", 98746);
//fd.append("payloadFile", files);
$http.post('http://localhost:49556/api/purchaseOrder/addReceiptDocument',
fd, { headers: { 'Content-Type': undefined } }
).then(function (response) {
alert('success');
})
}
<form name="oForm" id="oForm">
File: <input type="file" id="payloadFile" name="payloadFile" /><br />
<input type="submit" value="Submit" ng-click="submit(files)" />
</form>
public class receiptDocument
{
public int RDID { set; get; }
public int RECEIPTID { set; get; }
public byte[] DOCUMENT { set; get; }
}