views:

963

answers:

3

I have a View in which the user is able to upload a file to the server.

In this view I also have 2 buttons: one to Upload a file and other to Download the last file imported.

In my Controller I created 2 action methods: Import and Export.

How could I manage to redirect each button click to the proper action method in my Controller?

I have tried Html.ActionLink:

<%= Html.ActionLink("Upload", "Import", "OracleFile")%>
<%= Html.ActionLink("Download", "Export", "OracleFile")%>

Html.ActionLink didn't do the trick. The action links were taking me to the right Action methods but they were generating a GET request. This way Request.Files.Count = 0.

I need a POST request.

Note: the most intriguing part is that the upload was working and all of sudden it stopped working. I've seen that some people are having the same problem with FileUpload tasks in which the Request.Files is always Empty. I think it's empty because you need a post to the server. Isn't it?

+1  A: 

To generate a POST request for the upload, use the File Input form element and just post back to the server ala normal.

http://www.w3schools.com/jsref/dom_obj_fileupload.asp

Have a look at this blog post from Scott Hanselman. http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx

Alastair Pitts
I've already seen Scott Hanselman post but it didn't help.
Leniel Macaferi
OK, can we see a bit more of your code then? What does the controller action look like?
Alastair Pitts
Alastair, thanks for your help. The answer CoffeeCode provided did the trick.
Leniel Macaferi
+2  A: 

You have to use a "multipart/form-data" form, and submit the form. No ActionLink.

<form enctype="multipart/form-data" method="post" action="/Media/Upload/Photo">
    <input type="file" name="file" id="file" /> 
    <input type="submit" value="Upload" />
</form>
Dustin Laine
I have enctype="multipart/form-data" set but it didn't help.
Leniel Macaferi
You cannot use ActionLink to post, it must be a submit button.
Dustin Laine
Yes, I learned this too. This motivated me to ask this question.
Leniel Macaferi
I have 2 buttons and each onde should call an action method in the controller. Passing the action in the form tag doesn't work in my case.
Leniel Macaferi
I curious can the file be posted to the server with the help of jquery $.post(...) ??
CoffeeCode
I also thought about using jQuery, but how could I do that? I need to post and call a specific action method in each button.
Leniel Macaferi
U can write action in the controller that take a formcollection as a param, and the check which submitbutton was clicked. and then depending on that return the appropriate action
CoffeeCode
+3  A: 

maybe this will give u the idea:

view:

<form enctype="multipart/form-data" method="post" action="/Media/Upload/Photo">
    <input type="file" name="file" id="file" /> 
    <input type="submit"  name= "submitImport" value="Upload" />
    <input type="submit" name = "submitExport"  value="Download" />
</form>

controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Action (FormCollection formCollection)
        {
            if (formCollection["submitImport"] != null)
            {
                return Import(formCollection);
            }
             if (formCollection["submitExport"] != null)
            {
                return Export(formCollection);
            }
        }

the Export and Import are the appropriateactions

CoffeeCode
I got the idea, but formCollection is always empty. When I iterate its Key collection there's nothing there.
Leniel Macaferi
have u added the names and the ids to the submit buttons?i also added an attribute to the action
CoffeeCode
Yes, I added name and id to each submit button.
Leniel Macaferi
formCollection["submitExport"] does not return a bool value to be used in an if statement...
Leniel Macaferi
Oh, sorry... check the answer update
CoffeeCode
Hey CoffeeCode... this did the trick! Thank you very much. :) How I love StackOverflow and its users. You make this thing one of the best places of the entire internet. It's great to be a part of it.
Leniel Macaferi