javascript 如何使用 nervgh 的 Angular-File-Upload 发送 formData

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26350299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:51:33  来源:igfitidea点击:

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

javascriptasp.net-mvcangularjs

提问by Aaron Salazar

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

我在https://github.com/nervgh/angular-file-upload使用 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 preOpKeyalong with the file so that I can save the file to the appropriate record in the database.

当我硬编码preOpKey. 我想要做的是preOpKey随文件一起发送,以便我可以将文件保存到数据库中的相应记录中。

angular-file-upload has a fileDatain 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.

angular-file-uploadfileData在我正在填充的 API 中有一个,$scope.OnPreinspectionSubmit()但由于某种原因,我SaveFile()在 MVC 控制器中调用一次后找不到该值。

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 fileDatabut that isn't required to answer my question.

我只需要知道如何将值与我的文件一起传递,然后在我的 MVC 控制器中访问它。我想将它发送到 中会很方便,fileData但这不是回答我的问题所必需的。

Here is my Angular controller:

这是我的 Angular 控制器:

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:

这是我的 MVC 控制器:

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

亚伦

回答by ScionOfBytes

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

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