I am programming a web-service with Asp.Net MVC4. I am using WinForms for the Client.
I have implemented a SearchController that is able to return a list of Items.
[HttpGet]
public IEnumerable<Shared.Item> ByTag(string search)
{
ModelDbContext db = ModelDbContext.Current;
db.Items.Load();
//find some items....
return itemList;
}
I am calling it like this:
public Task<IEnumerable<Item>> SearchByTag(string tag)
{
client.BaseAddress = serviceAdress;
var getStuffCall=client.GetAsync("Search/ByTag/" + tag);
var r=getStuffCall.ContinueWith(
t =>t.Result.IsSuccessStatusCode? (t.Result.Content.ReadAsAsync<IEnumerable<Item>>().Result):new List<Item>()
);
return r;
}
This works fine. Now I would like to upload also an Item to the server. The problem is, that my type Item is structured and also contains a list of files and a list of pictures. As far as I understood, this does not work with a json-object. Or can I somehow wrap/encode my files and pictures?
From WinForms, you’re likely going to want to use WebClient, which allows you to POST multi-part forms to the server.
Just remember that HTTP wasn’t really designed for file transport, so just consider that if you have a whackload of files to push.
Finally on the controller you’re going to want to do something like this: How To Accept a File POST
There are a couple of relevant links there and some good samples.
Hope this helps a little more.