kravits88
kravits88

Reputation: 13049

Send post data with file upload AJAX

I am currently sending a file via AJAX like this:

var fd = new FormData();

    //do stuff to that adds files

    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/Upload/' + ID);
    xhr.send(fd);

And using the file in my contoller like:

HttpPostedFileBase file = Request.Files[i];

This all works fine but I want to send more information. How can I post more variables along with the file?

Upvotes: 0

Views: 215

Answers (2)

kravits88
kravits88

Reputation: 13049

var fd = new FormData();

    //do stuff to that adds files
    fd.append("Variable1", "data");
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/Upload/' + ID);
    xhr.send(fd);

This worked best for me.

Upvotes: 1

Zeeshan Hyder
Zeeshan Hyder

Reputation: 518

use variable name in xhr.send(). Like xhr.send(var1=fd&var2=anotherdata). Then at your server side create two POST data handlers, namely var1 & var2. One will hold fd and other will hold extra data.

Upvotes: 1

Related Questions