0

I was using this post to help me upload files to my MVC controller. jQuery ajax upload file in asp.net mvc For some reason the controller is never getting called. I would comment on the link but I don't have enough points for stackoverflow to let me comment on stuff. My javascript code is below, besides for changing a few things to use jquery is should have the same functionality. Any ideas? If more information is needed, just comment and I'll add whatever else I can. Thanks!

Javascript:

 $("#btnSubmit").click(function () {
        var formdata = new FormData();
        var fileInput = document.getElementById('fileInput');
        formdata.append(fileInput.files[0].name, fileInput.files[0]);
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/Index/Upload');
        xhr.send(formdata);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                alert(xhr.responseText);
            }
        }
        return false;
    });

Edit: When looking at the console output on my browser I saw the it wasn't recognizing the action(Index) that I was putting in. It would recognize the controller(Upload) but I think that is because the current view was already in the controller. Any ideas how to fix the path input for the XMLHttpRequest?

Community
  • 1
  • 1
sjr59
  • 93
  • 1
  • 9
  • What do you see in the network tab in your browser's console? – epascarello Apr 08 '16 at 13:09
  • Sorry, could detail where to find that? When I run the code it just reloads the view page. – sjr59 Apr 08 '16 at 13:11
  • If it just reloads, than my guess is the click event is not connected to the button. – epascarello Apr 08 '16 at 13:14
  • I just put an alert in the beginning and it went off, so the button click works fine – sjr59 Apr 08 '16 at 13:15
  • So are there any errors in the browser's developer console? (you probably need to click the checkbox to preserve logs if the page is reloading) Other option do `.click(function (event) { event.preventDefault();` – epascarello Apr 08 '16 at 13:16
  • Is it a path issue? Your POST url is absolute from server root. – Ryan Apr 08 '16 at 13:18
  • i says "The code on this page disabled back and forward caching" then on a new line "Navigation occurred." – sjr59 Apr 08 '16 at 13:20
  • @Ryan I think it may be a path issue, I think it isn't recognizing my view name in the path. Any ideas why/how to fix? – sjr59 Apr 08 '16 at 13:21
  • @epascarello when I add the event preventDefault the page just does nothing. – sjr59 Apr 08 '16 at 13:24
  • Can you verify that this entire function is run, at all. Add alert("button clicked") to the begining of the function – Dellirium Apr 08 '16 at 14:59
  • i did, the event works fine, it just isn't using the action i called in the open parameters – sjr59 Apr 08 '16 at 15:13

0 Answers0