File Upload in Angular 4

前端 未结 6 575
我在风中等你
我在风中等你 2021-02-04 14:26

when I\'m trying to install \"npm install ng2-file-upload --save\" in my angular 4 application it throws

UNMET PEER DEPENDENCY @4.1.0
UNMET PEER DEPENDENCY          


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 15:02

    import fileupload from primeng or use simple file uploader

    HTML

       
    

    TS

     var data = new FormData();
            let index: number = 0;
            if (this.files != undefined)
            {
                for (let file of this.files.files)
                {
                    data.append("myFile" + index, file);
                    ++index;
                }
            }
         data.append('viewModel', JSON.stringify(<>));
    

    Send this data with request return this._httpClient.post('api/controller', data);

    Server

      [HttpPost]
            public async Task Post()
            {
                HttpPostedFile httpPostedFile = null;
                var viewModel = JsonConvert.DeserializeObject(HttpContext.Current.Request["viewModel"]);
                if (viewModel != null)
                {
                    if (HttpContext.Current.Request.Files.AllKeys.Any())
                    {
                        var cnt = HttpContext.Current.Request.Files.Count;
                        for (int i = 0; i < cnt; i++)
                        {
                            httpPostedFile = HttpContext.Current.Request.Files["myFile" + i];
                        }
                    }
                }
            }
    

提交回复
热议问题