1

I'm quite new to the asp.net world, so I would appreciate any help or different solutions.

Does anybody know of a way to pass the PostedFile (HttpPostedFile) of a FileUpload via javascript/jquery to a server side method?

Client-side? //how do I pass the FileUpload.PostedFile?

codebehind:

[System.Web.Services.WebMethod]
public static void SaveMyFile(HttpPostedFile myFile)
{
// code to save file here
}

NOTE: I don't want to make use of plugins or any other thirdparty controls.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Stephen
  • 1,532
  • 1
  • 9
  • 17
  • Not sure what you want to do but have a look at the answer on this SO post... http://stackoverflow.com/questions/16689461/fileupload-postedfile-to-pagemethods-sever-side-method – Paul Zahra May 22 '13 at 10:34
  • @PaulZahra you have posted this SO link.. – Zaki May 22 '13 at 10:39
  • @Paul, your link is pointing at my post. anyhow, basically what I want to do is to create my own file upload control. and save the file without causing postback, hence making use of pagemethods – Stephen May 22 '13 at 10:41
  • Gah sorry... http://stackoverflow.com/questions/2241545/how-to-correctly-use-the-asp-net-fileupload-control – Paul Zahra May 22 '13 at 13:24

2 Answers2

0

This script can be used to post a file,

<script type="text/javascript">
function send() {
    var fd = new FormData();
    fd.append("fileToUpload", document.getElementById('filecontrol').files[0]);
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "url");
    xhr.send(fd);
      }
</script>
<input type="file" id="filecontrol" />
<input type="button" onclick="send()" value="Upload File" />

and in server side you can get the posted files by using

HttpPostedFile hpf = Request.Files[0];
Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
  • Hi Wings of Fire, thank you for the reply, much appreciated. I am getting the error 'FormData' is undefined. any idea how to resolve it? – Stephen May 22 '13 at 11:05
  • seems like it might be related to the fact that I am using IE 9. From a quick search on google found that FormData only works on IE10. – Stephen May 22 '13 at 11:20
0

Sorry about my mistaken comment earlier... anyway I believe this may well be what you want... ajax file upload using jquery file upload plugin... the jQuery File Upload plugin is pretty sexy

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76