简体   繁体   中英

File Upload with single Button

I have imlepmented file upload with one button i have taken one button and one file upload control and on clicking the button i am opening file upload for browsing file,

after selecting file i have to submit the form, the problem is arising when i click on submit,the file upload gets empties and i got HttpPostedFileBase null in my controller,but when i directly use file upload control(ie clicks on its browse button) its working fine

Below is my code

  $(document).ready(function () {
        $("#btn").click(function () {
            $("#logup").click();
            $("#logup").select();
            return false;
        });

    });




<input type="button" id="btn" value="Choose file" />
<input type="file" id="logup" name="logofile" style="display:none" />


<button type="submit">Submit</button>

2nd i also wants to show file name after selecting the file, besides my btn how can i do this?

Answer To Question 1

Below is a form with two submit buttons. Note that both these submit buttons have the same name ie “submitButton”

@Html.BeginForm("MyAction", "MyController"); %>
<input type="submit" name="submitButton" value="Button1" />
<input type="submit" name="submitButton" value="Button2" />
}

Now over to the Controller, the Action takes in an input parameter called string stringButton and the rest is pretty self-explanatory.

public ActionResult MyAction(string submitButton) {
        switch(submitButton) {
            case "Button1":
               // do something here
            case "Button2":
               // do some other thing here
            default:
                // add some other behaviour here
        }
...
}

Answer To Question 2

You will be getting fileName from HttpFilePostedBase object, but since you dont want a post back, you can create an action thats return this value as JSON

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM