1

How can i make the upload file action automatically once the files selected. means that the action will go to index action without pressing any button? Thanks in advance..

enter image description here

View

<form action="@Url.Action("Index", "Home")" method="post" enctype="multipart/form-data">
    @Html.AntiForgeryToken()
    <table >
        <tr>
            <td>
                <label>Name:</label>
                <input type="text" name="faxnumber" />
            </td>
        </tr>
        <tr>
            <td>
                <label>Select File:</label>
                <input type="file" name="files" id="file" multiple="multiple" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="submit" value="Send" id="btnSubmit" />
            </td>
        </tr>
        @ViewBag.Message
    </table>
</form>

javascript

<script>
    document.getElementById("file").onchange = function () {
        document.getElementById("form").submit();
    }
</script>

Controller

public ActionResult Index(IEnumerable<HttpPostedFileBase> files)
{
  //some code here
}

UPDATE

After selected files, the files will auto uploaded and the details will be stored in object name Document then back to the view with fail or success message. Then when I hit Send button all details will pass to the Send action in controller. How can I manage this process? The upload part is done already..but now I'm stuck in how can I send all the details to the next action in controller.

 public class Document
  {
    public string Name { get; set; } 
    public byte[] Binary { get; set; }
    public string Path { get; set; }
  }

SendController

public ActionResult Send(Document doc)
    {
      //some code here
    }
User123
  • 71
  • 1
  • 3
  • 12

1 Answers1

0

Your code will work fine if you change your submit button name value to anything other than "submit".

 <input type="submit" name="notsubmit" value="Send" id="btnSubmit" />

This is an interesting behavior ! I really do not know why it does not submit when the name attribute value is "submit". I will update the answer when i get any more info.

Shyju
  • 214,206
  • 104
  • 411
  • 497