0

I am converting web application to mobile app. We were using AjaxAsyncFileUpload in web application to save a document to the server, where AjaxAsyncFileUpload use to do the work for me. This was the code

Dim fileData As Byte() =new Byte(AjaxAsyncFileUpload.FileContent.Length-1){}
AjaxAsyncFileUpload.FileContent.Read(fileData, 0, fileData.Length)

InvestmentDeclare.DocSize  = AjaxAsyncFileUpload.FileContent.Length
InvestmentDeclare.DocFileName = AjaxAsyncFileUpload.FileName
InvestmentDeclare.DocFileType = AjaxAsyncFileUpload.PostedFile.ContentType
InvestmentDeclare.Document = fileData

And then simply save this to my database.

Now while Converting this to mobile app (I am also using c# for mobile app), I am not able to pass the byte array. I am using fiddler for testing. I have attached an image of how I'm passing it through fiddler. In my API POST method I'm getting a null value to my document variable while I'm able to get rest of my values properly.

What could be the issue? Am I not passing the byte in proper Json format?

In API:

public class AddInvestmentDeclare
{
 public int EmployeeId { get; set; }
 public int YearId { get; set; }
 public int InvestmentId { get; set; }
 public List<EmpDocument> EmpDocuments { get; set; }
}

public class EmpDocument
{
 public byte[] Document { get; set; }
 public string DocumentFileName { get; set; }
 public long DocumentSize { get; set; }
 public string DocumentType { get; set; }
}

public HttpResponseMessage Post(int YearId, [FromBody]List<AddInvestmentDeclare> InvestmentDeclared)
{

When I check my InvestDeclared list on run time I see that document variable is not filled and it is showing null. I have attached an image of that as well.

Matt
  • 1,245
  • 2
  • 17
  • 32
Tausif Khan
  • 31
  • 2
  • 9
  • Anyone who can help me with this please? – Tausif Khan Oct 15 '18 at 05:40
  • What is the application that is actually calling the post method? Show *that* code please. Fiddler is good for testing and to properly upload a file use https://stackoverflow.com/a/51622705/1260204. To read that file in, if it is passed correctly, see https://stackoverflow.com/questions/10320232/how-to-accept-a-file-post. – Igor Nov 14 '18 at 10:37
  • Have you tried sending the byte array as string? I've found some example that I used for testing and the JSON that I'm using looks like this: { "ID":46, "Content":"JVBERi0xLjQNCjEgMCBvYm......" } Also, try converting it with this https://stackoverflow.com/a/11654825/9233618 – Matt Nov 14 '18 at 13:02
  • Also, why don't you set up API method in a way that it accepts just one object containing all data, instead of each parameter individually? – Matt Nov 15 '18 at 12:16
  • @Matt : Yes of course i can do that. – Tausif Khan Nov 16 '18 at 06:12
  • @TausifKhan Any success? – Matt Nov 19 '18 at 09:31
  • @Matt : Yes Matt. As you said combine it all in one object and so i did. I combined it all in one object and passed it through a POST method. And about my issue, I converted the file into base64 string and on my end i converted it back to byte[] array. Which worked for me. Thanks Matt. – Tausif Khan Nov 19 '18 at 09:52
  • @TausifKhan I will write it down as an answer so it is visible to others. I would appreciate if you would accept it as solution. – Matt Nov 19 '18 at 10:14

1 Answers1

0

First, setup your API method in a way that it accepts just one object containing all data instead of each parameter individually.

Instead of:

public HttpResponseMessage Post([FromUri]string AccordDbName, [FromUri]String PayCareDbName, [FromUri]...)

Set it up like:

public HttpResponseMessage Post(Data dataObject)

Secondly, try sending file as a base64string. Here is example how to do it.

Your file in JSON request should look something like this:

{ "ID":46, 
  "Content":"JVBERi0xLjQNCjEgMCBvYm......"
} 
Matt
  • 1,245
  • 2
  • 17
  • 32