Sending byte array to API - asp.net-web-api2

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.

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......"
}

Related

How to implement GET endpoint in ASP.NET Core which returns object with File?

I have the following class:
public class Person
{
public string Name { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public byte[] PersonPhoto{ get; set; }
}
Usually I implement get objects endpoint, which returns not directly file but path to that file, and second endpoint to retrieve file separately.
Is that any solution which will let me implement it from one endpoint?
Is it a good practice?

Postman. Issue with multipart/form-data (415 Unsupported Media Type)

I got some issue during test my endpoint (with multipart/form-data) on Postman.
I have following method (some fields + photo). This method works with Swagger ok.
[HttpPost]
public async Task<bool> MailPhoto([FromForm] MailwithPhoto mailWithPhoto)
{...}
public class MailwithPhoto
{
public string mail_message { get; set; }
public IFormFile photo_file { get; set; }
public string userContact { get; set; }
public string category { get; set; }
public string userName { get; set; }
public string method { get; set; }
}
enter image description here
First need to change Key message to mail_message,or mail_message will not be binded.
415 is often due to Content-Type or Content-Encoding, or as a result of inspecting the data directly.
Here is a working demo(I use a .net core mvc project):
result:

Json parsing in windows phone (C#)

I am new to Windows Phone development.I want to parse some data using json parsing.After
googling i find a number of example but I am not able to understand properly.I have follow
a link
http://dotnetbyexample.blogspot.in/2012/01/json-deserialization-with-jsonnet.html
to do json parsing But i am not able to show Storage ,Memory, ScreenSize in my List and my code is
private void Load_Click(object sender, RoutedEventArgs e)
{
var w = new WebClient();
Observable
.FromEvent<DownloadStringCompletedEventArgs>(w, "DownloadStringCompleted")
.Subscribe(r =>
{
var deserialized =
JsonConvert.DeserializeObject<List<RootObject>>(r.EventArgs.Result);
PhoneList.ItemsSource = deserialized;
});
w.DownloadStringAsync(
new Uri("http://www.schaikweb.net/dotnetbyexample/JSONPhones1.txt"));
}
}
public class Specs
{
public string Storage { get; set; }
public string Memory { get; set; }
public string Screensize { get; set; }
}
public class RootObject
{
public string Brand { get; set; }
public string Type { get; set; }
public Specs Specs { get; set; }
}
}
Please Help me to solve this issue or any other sample for the same is also appreciated
Thanks
The code seems ok, so if you aren't running in to any errors there might be an issue with your UI code or UI to data coupling.
You need to post the UI code to get some help with this I suspect.

ASP.NET WebApi form-urlencoded string deserialize

I develop service with WebApi and Client which send to this service gzipped data by POST method (to save bandwidth). Both of them over my control. On server I receive compressed data, decompress It and have string such as:
section[0][caption]=Foo&
section[0][address]=175896&
section[0][counters][]=2&
section[0][counters][]=2&
section[0][errors][]=ERR_NOT_AVAILABLE&
errors=true&
trmtimestamp=1346931864358
ie simple www-form-urlencoded string.
Does ASP.NET MVC4 WebApi have some method to bind or deserialize this string to my Model?
public class Snapshot
{
public List<SectionState> sections { get; set; }
public bool errors { get; set; }
public double date { get; set; }
public double trmtimestamp { get; set; }
}
You may use the following code to parse the string:
new System.Net.Http.Formatting.FormDataCollection(query).ReadAs<Snapshot>();
The ReadAs is extension method defined under System.Web.Http.ModelBinding.FormDataCollectionExtensions.

Rolling my own CMS with the WCF, what should my contract be?

I'm looking to create a solution for a asp.net mvc app that displays unstructured course descriptions that have been scraped from around the web. The web server will be on a web hosting company while the description db will be on a private network, so I'd like to pass the description to the mvc app through a WCF service. For the body of the description do I just want to use a string property...
public class CourseDescription {
// data fields
public string CourseTitle { get; set; }
public int Days { get; set; }
public List<DateTime> UpcomingDates { get; set; }
// html properties
public string Keywords { get; set;}
public string HtmlDescription { get; set; }
}
... or is there a better way?
If it is unstructured text, a string is fine.