0

i have following code i want to upload excel file and save to server. but this function not working it show me 500 (Internal Server Error) what's wrong in my code please help me this any professional i spend 3 hours but not success . i was google but not success simple i want to upload file through angularjs or ajax call

C# Code not call from AJAX i think this that's why its show error

HTML

  <input type="file" id="uploadInput" />
 <button ng-click="SubmitForm()" class="btn btn-info">Submit</button>



 AppModel.controller('ImportExcelCtrl', ['$scope', '$filter', '$http', function ($scope, $filter, $http) {
        $scope.SubmitForm = function () {
            var element = document.getElementById("uploadInput");
            var file = element.files[0];
            console.log(file.name);
            console.log(file);
            var loFormData = new FormData();
            loFormData.append("filename", file.name);
            loFormData.append("file", file);

            var loAjaxRequest = $.ajax({
                cache: false,
                type: 'POST',
                url: "/api/myapi/UploadFileExcel",
                contentType: false,
                processData: false,
                data: loFormData,
                headers: {
                    'Content-Type': 'multipart/form-data'
            }
            });

            loAjaxRequest.done(function (xhr, textStatus) {
                alert(textStatus);
            });
        };
    }]);

CS

[HttpPost]
        public async Task<HttpResponseMessage> UploadFileExcel()
        {
            if (!this.Request.Content.IsMimeMultipartContent())
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            try
            {
                var loProvider = new MultipartFormDataStreamProvider(Path.GetTempPath());

                await Request.Content.ReadAsMultipartAsync(loProvider);

                string lsFilename = loProvider.FormData.GetValues("filename").First();
                var loFile = loProvider.FileData.First();
                string lsFileContent = File.ReadAllText(loFile.LocalFileName);

                return new HttpResponseMessage(HttpStatusCode.OK);
            }
            catch (Exception exp)
            {
                return new HttpResponseMessage(HttpStatusCode.InternalServerError);
            }
        }
3
  • You need to see logs of your server. That will give you more hints than people here can. Commented Aug 9, 2018 at 21:22
  • its shows 500 internal server error .but cs side function not call Commented Aug 9, 2018 at 21:24
  • yes, but you need to see the logs of the server so you see the reason for the 500. Commented Aug 9, 2018 at 21:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.