如何在AngularJS和webApi中使用参数发布文件

时间:2016-05-23 11:33:15

标签: c# angularjs asp.net-web-api

我想将带有参数的文件发布到Web Api函数,我可以在删除参数时将文件保存到服务器上的目录但是使用参数post该方法不调用

这是angularJs

   // this will get the upload from the html
        $scope.getFileDetails = function (e)
        {
            $scope.files = [];
            $scope.$apply(function ()
            {

                // STORE THE FILE OBJECT IN AN ARRAY.
                for (var i = 0; i < e.files.length; i++)
                {
                    $scope.files.push(e.files[i])  
                }

            });
        };

       //the file array 
        $scope.uploadFiles = function()
        {
            //FILL FormData WITH FILE DETAILS.
            var data = new FormData();
            for (var i in $scope.files)
            {
                data.append("uploadedFile", $scope.files[i]);  
            }    
            // ADD LISTENERS.
            var objXhr = new XMLHttpRequest();
            objXhr.addEventListener("progress", updateProgress, false);
            objXhr.addEventListener("load", transferComplete, false);

            var empID="PC145"
            // SEND FILE DETAILS TO THE API.
            objXhr.open("POST", "/api/Logo/"+empID); // this is not working
            objXhr.send(data);
        }

这是网络Api方法

public string UploadFiles(String empid)
        {
            int iUploadedCnt = 0;

             String na = empid; 
            // DEFINE THE PATH WHERE WE WANT TO SAVE THE FILES.
            string sPath = "";
            sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Logo/");

            System.Web.HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;

            // CHECK THE FILE COUNT.
            for (int iCnt = 0; iCnt <= hfc.Count - 1; iCnt++)
            {
                System.Web.HttpPostedFile hpf = hfc[iCnt];
                String fileName = sPath + Path.GetFileName(hpf.FileName);
                String fileNameDB = Path.GetFileName(hpf.FileName);
                this.FileName = fileNameDB;
                if (hpf.ContentLength > 0)
                {
                    // CHECK IF THE SELECTED FILE(S) ALREADY EXISTS IN FOLDER. (AVOID DUPLICATE)
                    if (!File.Exists(fileName))
                    {
                        // SAVE THE FILES IN THE FOLDER.

                        hpf.SaveAs(fileName);
                        insertLogo(fileNameDB);
                        iUploadedCnt = iUploadedCnt + 1;
                    }
                }
            }

            // RETURN A MESSAGE (OPTIONAL).
            if (iUploadedCnt > 0)
            {

                return iUploadedCnt + " Files Uploaded Successfully";
            }
            else
            {
                return "Upload Failed";
            }
        }

使用参数函数不调用,如何使用参数发布文件。

0 个答案:

没有答案