0

I am completely new to coding so forgive me if this question is stupid. I am trying to add a file upload button on a html page for the user to upload the file onto the server. The project uses html, angularjs and the api is in c#.

My HTML code is as follows:

<div ng-controller="MyCtrl">
<input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/>
</div>

My Angularjs function within the controller is:

$scope.uploadFile = function(files) {
      var fd = new FormData();
      //Take the first selected file
      fd.append('file', files[0]);
          $http({
            method: 'POST',
            url: '/Users/x/Documents/api/FileController.cs',
            withCredentials: true,
            headers: {'Content-Type': undefined },
            transformRequest: angular.identity
      }).success(function(){alert('Success')}).error(function(){alert('Error')});
  };

In the FileController.cs file in asp.net I have a function:

[HttpPost]
    public ActionResult Index(HttpPostedFileBase file) {

      if (file.ContentLength > 0) {
        var fileName = Path.GetFileName(file.FileName);
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
        file.SaveAs(path + FileName);
      }

      return RedirectToAction("Index");
    }

The http post keeps showing an error.

My questions are:

  • Is the format of this url correct? url: '/Users/x/Documents/api/FileController.cs'
  • Can a http post to a .cs file?
  • Is there something missing from the function of the function written in c#?
  • Is a page load required in c# to receive the post?
2
  • post the exception message Commented May 6, 2015 at 7:57
  • You need the action url, not the path like this "url: '/Users/x/Documents/api/FileController.cs'," For example : "localhost:8080/api/File" Commented May 6, 2015 at 7:59

1 Answer 1

1

You don't access the cs-file by post, but the method. So the url should end with /File:

/Users/x/Documents/api/File

First try to test using a get method which you can access directly through your browser. If that works, go on with your post

Sign up to request clarification or add additional context in comments.

1 Comment

Actually just /File since Controller is not a part of routing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.