views:

18

answers:

1

I have a functional page posting data to a Page WebMethod

[WebMethod()]
public static string sayHello(string pTest, string pText)
{
  return pTest + " - " + pText;
}

which I call using this jQuery

$(document).ready(function () {
  $("#sayHelloButton").click(function (event) {
    var name = $('#name').val();
    var text = $('#text').val(); 
    var dataString = JSON.stringify({
      pTest: name,
      pText: text
    }); 

    $.ajax({
      type: "POST",
      url: "page.aspx/sayHello",
      contentType: "application/json; charset=utf-8",
      data: dataString,
      dataType: "json",
      success: AjaxSucceeded,
      error: AjaxFailed,
      beforeSend: AjaxStart,
      complete: AjaxEnd
    });
  });
});

but now it should also be possible to upload a file and here I am a bit lost.

From what I can read in different posts in here it is not possible but please correct me if I am wrong - but I have seen several jQuery plugins which make upload possible, e.g. through a Flash-script, but how will it then be possible to both post data and upload with only one click? Does anyone have any suggestions which upload plugin is best - and how i can succeed in upload and post in one action?

A: 

Can't tell for all plugins, but I'm using jQuery Form without any problems. No flash involved, it just creates hidden iframe instead.

Here's a demo.

but how will it then be possible to both post data and upload with only one click
The process is the same as with normal ajax request. You declare a form element, but instead of $('#myform').submit(); do $('#myform').ajaxSubmit().

Nikita Rybak
Please correct me if I am wrong, but if i do as you suggest and I use ASP.NET which have only 1 form-tag i guess it submits a lot of irrelevant stuff (e.g. viewstate and elements belonging to another part of the page) making the operation slower and that I cannot target a WebMethod with specific parameters as in my example?
keysersoze
@keysersoze By default, yes. I would try creating a hidden form from javascript and attach your file input to it (temporary). But never had to solve this problem, so no guarantees.
Nikita Rybak