Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good People,
I am working on a web application where a user uploads an excel file, clicks the upload button and sends the file with its contents to the server for processing. I am am using angular.js and web API technologies. Can anyone advise on how do I "POST" the file from the client application and accept on the server side?

1. This is the html code for the front end:
HTML
<div class="modal fade" id="uploadUsersModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-removed" style="width: 510px">
            <div class="modal-header">
                <h4 class="modal-title">Bulk Upload - Users</h4>
            </div>
            <div class="modal-body">
                <div class="form-group has-success has-feedback" style="width: 400px">
                    <label for="adminChecklist">File</label>
                    <input type="file" ng-model="model.file" class="form-control" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
                </div>
            </div>
            <div class="modal-footer">

                <div class="form-group has-success has-feedback" style="width: 440px">
                    <span class="pull-left">{{confirmation.confirmation}}</span><button type="button" class="btn btn-primary" ng-click="upload(model)">{{confirmation.buttonMessage}}Upload</button>
                    <button type="button" class="btn btn-danger" ng-click="closeUploadUsersModal()">Close</button>
                </div>
            </div>

        </div>
    </div>
</div>


2. This is javascript code in my controller that has to send the file to the server:
PenStatus is the controller on the viewerAPI.
JavaScript
var Files = $resource('viewerAPI/PenStatus/:id', { id: "@id" });

           angular.extend($scope, {

               model: { file: null },

               upload: function (model) {
                   Files.prototype.$save.call(model.file, function (self, headers) {
                       // Handle server response
                   });
               }
           });


3. This c# code on the server where I need to accept the excel file:
C#
// POST: api/PenStatus/5
       [HttpPost]
       public IHttpActionResult Post()
       {



           return null;
       }
Posted
Comments
Sreekanth Mothukuru 15-Jul-15 8:49am    
Check this forum on similar topic. You need to use FormData on client side to hold and post the selected file.

http://stackoverflow.com/questions/13963022/angularjs-how-to-implement-a-simple-file-upload-with-multipart-form
P.C Shabangu 15-Jul-15 10:09am    
@Sreekanth Mothukuru Ok I will try it out and see.
P.C Shabangu 16-Jul-15 3:23am    
Hi All, I had a look at another example and based on it I can send the file to the to the api without having the javascript code. here is the link:
http://www.c-sharpcorner.com/UploadFile/2b481f/uploading-a-file-in-Asp-Net-web-api/
P.C Shabangu 16-Jul-15 3:24am    
I am a bit unsure about it because I have my API url on the html page. Is that a problem at all?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900