danialfarid / ng-file-upload

Lightweight Angular directive to upload files with optional FileAPI shim for cross browser support
MIT License
7.87k stars 1.59k forks source link

Cannot Add additional data to upload request #2044

Open LenWhims opened 6 years ago

LenWhims commented 6 years ago

I want to add $scope.recipeID to the upload request to controller, I tried the following

Upload.upload({
                            url: '/Files/Upload/',
                            data: {
                                files: $scope.SelectedFiles,
                                'RecipeId': $scope.recipeID
                            }

then called it in the controller like so

[HttpPost]
        public ContentResult Upload()
        {
            string path = Server.MapPath("~/Uploads/");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            foreach (string key in Request.Files)
            {
                HttpPostedFileBase postedFile = Request.Files[key];
                postedFile.SaveAs(path + postedFile.FileName);

                Models.File recipefile = new Models.File();
                recipefile.Name = postedFile.FileName; //not saving
                recipefile.Path = path; //not saving
                recipefile.RecipeId = 0; //Error on .RecipeId
                db.Files.Add(recipefile);
            }

            return Content("Success");
        }

but it returned an error

HttpPostedFileBase does not contain a definition of 'RecipeId'

I tried adding another data like this

angular.forEach($scope.SelectedFiles, function (value, index) { 
        $scope.SelectedFiles[index]["RecipeId"] = $scope.recipeID; 
}); 

and then I check it in alert by JSON.stringify and this was the result

[{"RecipeId":0},{"RecipeId":0}]

but I still could not access it in Files/Upload/ controller.

and another problem I have is that postedFile.FileName and path is not saving to db, even file id was not saving. It should atleast save id since it's database generated but it doesn't, is this wrong Models.File recipefile = new Models.File(); ?

I have using System.IO; since I want it to auto create a dir if it is not available. I tried removing it but Id, Nameand Path is not saving.