1

i need upload file from

<input class="fromFileInput" id="VeryfyFromFileInput" type="file" fileread="fileContent" ng-click=uploadFile />

using angular

controller:

    $scope.fileInformation = [];

    $scope.uploadFile = function () {
        $scope.fileModel = $('#VeryfyFromFileInput');
        recordsService.passFile($scope.fileModel.context);
    };

service:

        this.passFile = function (data) {
        return $http({
            method: 'POST',
            url: url + "/GetFile",
            data: data,
            headers: { 'Content-Type': 'application/json' }
        });
    };

To ASP.NET MVC controller

    [HttpPost]
    public void GetFile([FromBody] HttpRequestMessage file)
    {

    }

But i have always null, so which type of parameters i must use instead of 'HttpRequestMessage '? Or maybe problem in JS side?

1
  • Have you check whether or not data contains anything? Commented Oct 19, 2016 at 17:38

1 Answer 1

1

You cannot pass bytedata over the wire as application/json. One solution is to use a Form Data object and make the ajax call as a multipart/formdata. Use the following Ajax call.

         $.ajax({
                url: 'api/controller/action',
                processData: false,
                contentType: false,
                type: 'POST',
                data: formDataObject,
                success: function () {
                    alert('yay!');
                },
                error: function () {
                    alert('nay :c');
                }
            });

I used angular-ui file upload directive. I'm not sure if the way you're accessing it works, if it does then you can simply use the following syntax to add your file to FormData

var formDataObject = new FormData();
formDataObject.append('file', $scope.fileModel;

On the server use this snippet to access your data

 var filesReadToProvider = await Request.Content.ReadAsMultipartAsync();
 foreach (var stream in filesReadToProvider.Contents)
 switch (stream.Headers.ContentDisposition.Name)
                {
                    case "\"file\"":
                        byte[] fileData = await stream.ReadAsByteArrayAsync();
                        break;
                    default:
                        break;

                }

            }

For information on how to write a byte array as file to server follow: Write File to server

Good luck!

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

3 Comments

Can you little bit more describe
about filesReadProvider. I don't want read content of file, i need just create the same file in my directory(on server)
I have updated my answer. Please accept and upvote if it was helpful. Good luck!

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.