lundi 1 juin 2015

redirecting the request to the same page after upload in mvc

I am relatively new to upload in MVC, so I have a little clarification. I am performing an ajax upload operation.

The upload operation works absolutely fine, but after the upload, it is redirecting to a new page which is blank page which I do not know how to stop it from doing it. I used EmptyResult as well, but still the same thing is happening.What is it that I am missing?

@using(Html.BeginForm())
{
 <input type="file" id="fileInput" />
<button onclick="startUpload();">Upload</button>
<br />
<progress id="progressBar" max="100" value="0" />
}
<script>



    function startUpload() {
        var fileInput = document.getElementById("fileInput");

        if (fileInput.files.length == 0) {
            alert("Please choose a file");
            return;
        }

        var progressBar = document.getElementById("progressBar");
        var xhr = new XMLHttpRequest();

        xhr.upload.onprogress = function (e) {
           // alert("in progress");
            var percentComplete = (e.loaded / e.total) * 100;
            progressBar.value = percentComplete;
        };

        xhr.onload = function () {

        };
        xhr.onerror = function () {

            alert("Error! Upload failed. Can not connect to server.");
        };
        xhr.onreadystatechange = function () {

        };
        progressBar.value = 0;
        xhr.open("POST", "/Home/Index", true);
        xhr.setRequestHeader("Content-Type", 'application / json');
        xhr.send(fileInput.files[0]);
    }
</script>


    [HttpPost]
    public EmptyResult  Index(FormCollection frm)
    {
        // Create JSON Response
        var jsonData = new
        {
            status = "success",
            message = "message"
        };
        return null;
                }

Aucun commentaire:

Enregistrer un commentaire