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);
            }
        }
Spider
  • 23
  • 2

0 Answers0