简体   繁体   中英

How to send formData with nervgh's Angular-File-Upload

I am using nervgh's Angular-File-Upload at https://github.com/nervgh/angular-file-upload

It is working like a charm when I hard code the preOpKey . What I would like to do is to send the preOpKey along with the file so that I can save the file to the appropriate record in the database.

angular-file-upload has a fileData in the API which I'm populating in $scope.OnPreinspectionSubmit() but for some reason I can't find that value once SaveFile() in my MVC controller is called.

I just need to know how to pass a value along with my file and then access it in my MVC controller. I guess it would be convenient to send it in the fileData but that isn't required to answer my question.

Here is my Angular controller:

var OperatorPreinspectionControllers = angular.module('OperatorPreinspectionControllers', ['angularFileUpload']);

OperatorPreinspectionControllers.controller('OperatorPreinspectionCtrl', ['$scope', '$http', 'FileUploader',
    function ($scope, $http, FileUploader) {

        $scope.uploader = new FileUploader({
            url: pageBaseUrl + 'image/SaveFile'
        });

        $scope.uploader.filters.push({
            name: 'imageFilter',
            fn: function (item /*{File|FileLikeObject}*/, options) {
                var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
                return '|jpg|png|jpeg|bmp|gif|pdf|'.indexOf(type) !== -1;
            }
        });

        // Send the form data to the database
        $scope.OnPreinspectionSubmit = function () {
            if (confirm("Are you sure you want to save this information?")) {
                $http.post(pageBaseUrl + 'api/PreInspectionForm', $scope.formInformation).success(function (returnData) {
                    $scope.uploader.formData.push({ preOpKey: returnData });
                    $scope.uploader.uploadAll(); // Upload file
                });
            } else { }
        }
    }
]);

Here is my MVC Controller:

public void SaveFile()
    {
        HttpFileCollectionBase files = Request.Files;
        HttpPostedFileBase uploadedFile = files[0];

        var preOpKey = 123; // ???????

        // Turn the file into bytes
        byte[] data;
        using(Stream inputStream = uploadedFile.InputStream)
        {
            MemoryStream memoryStream = inputStream as MemoryStream;
            if(memoryStream == null)
            {
                memoryStream = new MemoryStream();
                inputStream.CopyTo(memoryStream);
            }
            data = memoryStream.ToArray();
        }

        PreOpManager.SaveImage(preOpKey, data, uploadedFile.FileName, uploadedFile.ContentType);
    }

Thank you,

Aaron

The one solution that has so far worked flawlessly for me has been:

$scope.uploader.onBeforeUploadItem = onBeforeUploadItem;

function onBeforeUploadItem(item) {
  item.formData.push({your: 'data'});
  console.log(item);
}

As per https://github.com/nervgh/angular-file-upload/issues/97#issuecomment-39248062

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM