1

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?

Kas
  • 3,747
  • 5
  • 29
  • 56
Katharina
  • 39
  • 5

2 Answers2

1

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.

Community
  • 1
  • 1
MisterJames
  • 3,306
  • 1
  • 30
  • 48
  • Thanks again. I looked at WebClient, but only found methods to upload a single file, string, byte-array or key-value pairs. In here http://stackoverflow.com/questions/7257957/how-to-use-webclient-uploadfileasync-to-upload-files-and-post-params-as-well they say that it not possible with w WebClient. Or has that changed? – Katharina Sep 10 '12 at 10:01
  • No, it looks like you're right. But that doesn't stop you from making multiple requests (1 for each file). As a last resort, you could ZIP the files up. – MisterJames Sep 11 '12 at 15:13
0

You're going to have to write a bunch of code yourself, or go with a jQuery plugin such as Uploadify. The reason for this is that XMLHttpRequest does not allow encoding/uploading of files through Ajax. This plugin uses several known work-arounds/solutions/failovers to make it work for you.

With this in place, you could consider making two calls, one for your object, and one for the files/images that you wish to push to the server.

Cheers.

MisterJames
  • 3,306
  • 1
  • 30
  • 48
  • Thanks for your answer. But as I am using WinForms for the Client, I cannot use Uploadify. Sorry that I forgot to mention the Winforms. – Katharina Sep 07 '12 at 11:49