DannyT
DannyT

Reputation: 638

asp.net mvc2 post file and form values from flash

We're trying to upload a file and submit parameters to an ASP.Net MVC2 controller from a flash application. Essentially though this is simply creating a standard multipart/form post with a file and posted params.

In the controller:

public string Upload(HttpPostedFile file, string someString, 
                        int someInt, DateTime someDate)
{
    // some code
   return "success";
}

And from flash(flex):

var file : FileReference = "C:\someFile.txt";
var urlRequest: URLRequest = new URLRequest("http://localhost/MySite/Uploader/Upload");
urlRequest.method = URLRequestMethod.POST;

var variables:URLVariables = new URLVariables();
variables.someString = "test";
variables.someInt= 1;
variables.someDate = "01/01/2011 00:00:00";

urlRequest.data = variables;

file.upload( urlRequest, "file" );

The controller is instantiated but the method isn't found, if we just post the file without the additional params it works fine and we can also get it to work with the file and the someInt param but nothing else?

Upvotes: 0

Views: 372

Answers (1)

DannyT
DannyT

Reputation: 638

For the benefit of others trying to do the same we managed to get this to work using:

[HttpPost]
public int Index(HttpPostedFileBase file,
    [Bind(Prefix = "someString")] string someString,
    [Bind(Prefix = "someInt")] int someInt,
    [Bind(Prefix = "someDate")]  string someDate)
{
    // stuff here
}

This works but I've no idea why, I thought prefixes were for accessing collection items or some such. I guess when posting a file the associated form data is passed in a collection? If anyone knows why or a better approach I'm happy to give the answer to someone else.

Upvotes: 1

Related Questions