0

As part of a data grid, I want the user to be able to upload a file. I accomplish this via a jquery ajax call to the server. Here's my ajax call:

var formData = new FormData();
formData.append("file", Data.File); //Data.File is a File object from a file input
$.ajax({
    type: "POST",
    url: "@Url.Action("UploadFile")",
    data: formData,
    contentType: false,
    processData: false
});

And, here's my controller action signature:

[HttpPost]
public IActionResult UploadFile()

When I test this locally via IIS Express, it works perfectly. But, when I publish to the IIS web server it only gives me 404 errors. I've researched possible causes on the IIS end, but they all seem to do with big files. The files I'm testing with aren't big; All the ones I've tested with are under 1 MB. I also have another page where I allow file uploads via ajax, and that page does not experience any problems locally or on the web server.

This is not the only ajax call being made on this page but it is the only one failing.

greenjaed
  • 589
  • 8
  • 20
  • So what is the URL being requested and what should it be? How do they differ? Check your browser's *Network* console – Phil Mar 09 '17 at 00:24
  • Here's the URL it requests: http://[site]/[subdomain]/DataGrid/UploadFile. That is exactly what it should be. – greenjaed Mar 09 '17 at 00:44
  • And that *should* work? If you send a cURL request, does it return a 404 status ~ `curl -v -X POST -H "Content-Type:" "http://[site]/[subdomain]/DataGrid/UploadFile"` – Phil Mar 09 '17 at 00:46
  • Alright, I did the above (except with Powershell and "Invoke-WebRequest -URI") and, yeah, I get a 404. – greenjaed Mar 09 '17 at 01:02
  • Sounds like you server-side application isn't configured correctly – Phil Mar 09 '17 at 02:10
  • Look my Ajax Asp.Net Core Demo - https://github.com/CShepartd/ASP.Net_Core_-_Ajax_Example – J. Doe Mar 09 '17 at 13:30
  • You were right @Phil, there was a problem on the server. I forgot to install software the site depended on. Installing the requisite software fixed the issue. – greenjaed Mar 09 '17 at 19:33

1 Answers1

1

At first, I was confused because, as I noted in the original post, I have a page that was doing the exact same operation that I'm currently having problems with. However, after a painfully long time, I realized the action could be returning a 404 because of some exception being thrown. So I did the rational thing and put a try-catch block within my code and returned any errors.

That's when I realized that though the calls were the same, I WAS doing something different on the backend. I had forgotten that the site relied on software that I had installed locally, but not on the web server. After installing that software on the server, the file uploaded normally.

tl;dr: The action returned 404 because it was throwing an exception due to missing software on the web server.

greenjaed
  • 589
  • 8
  • 20
  • Sounds like your exception handler is returning the wrong response code. This should have been a *"500 - Internal Server Error"* – Phil Mar 09 '17 at 21:58