读取从angular 7发送的webapi中的FormData键

时间:2019-07-10 13:46:58

标签: angular asp.net-web-api

我基本上是在angular 7应用程序中构建formdata,该数据将发送到asp.net Web api中的webapi方法以上传文件。这个想法是,在上传文件的同时,我还需要发送其他字段来存储数据。目前发生的是当我上传三个文件时,正在生成3个密钥,例如文件1,文件2等。 我如何一次发送记录。我需要将文件,documentTypeId和documentDate作为单个记录的一部分传递。我需要在键上附加多个值还是需要为每个字段创建单独的键

export interface IDocumentUpload {
    fileDropEntry: NgxFileDropEntry;
    name: string;
    selectedDocumentItem: { 'Id': number; 'Name': string; }
    selectedDate: Date;

}

    public files: IDocumentUpload[] = [];


    public createDocument() {

            const formData = new FormData();
            let inx = 1;
            let d = 1;
            let e = 1;

          this.files.map(doc => {

                formData.append('documentTypeId' + d++, doc.selectedDocumentItem.toString());
                formData.append('documentDate' + e++,  doc.selectedDate.toDateString());
                const fileEntry = doc.fileDropEntry.fileEntry as FileSystemFileEntry;
                fileEntry.file((file: File) => {
                    formData.append('file' + inx++, file, doc.name);
                });

            });

            this.documentUploadService.UploadDocument(formData).subscribe(data => {

            });
        }

如果我在客户端上执行console.log,我会得到正确的打印键

formData.forEach((value,key) => {
        console.log(key+" "+value)
      });

输出

enter image description here

但是在服务器端,HttpContext.Current.Request.Files仅显示两个键

[HttpPost]
        [SkipTokenAuthorization]
        [Route("api/documentupload/uploadDocument")]
        public IHttpActionResult uploadDocument()
        {
            HttpResponseMessage response;
            var mgrDocumentService = GetService<DOCUMENT>();

            var mgrStrategyDocument = new MANAGERSTRATEGY_DOCUMENT();
            var mgrStrategyDocumentService = GetService<MANAGERSTRATEGY_DOCUMENT>();
            List<int> documentIds = new List<int>();


            if (HttpContext.Current.Request.Files.Count == 0)
                return null;

            var attachmentIds = new List<int>();
            foreach (string fileName in HttpContext.Current.Request.Files)
            {
                var file = HttpContext.Current.Request.Files[fileName];
                if (file != null && file.ContentLength > 0)
                {
                    Stream fs = file.InputStream;
                    BinaryReader br = new BinaryReader(fs);
                    byte[] inputArr = br.ReadBytes((Int32)fs.Length);

                    //var inputArr = new byte[file.ContentLength];
                    //file.InputStream.Read(inputArr, 0, file.ContentLength);

                    var mgrDocument = new DOCUMENT
                    {
                        DOCUMENT_TYPE_ID = 238,
                        DOCUMENT_DATE = DateTime.Now.Date.AddDays(13),
                        NAME = file.FileName,
                        DOCUMENT_CONTENT = inputArr,
                        EXTENSION = GetFileExtension(file.FileName)
                    };

                    var a = mgrDocumentService.Create(mgrDocument);

                    documentIds.Add(mgrDocument.ID);
                    var mangerStrategyDocumentService = GetService<MANAGERSTRATEGY_DOCUMENT>();
                    var mgrStrategyDocument1 = new MANAGERSTRATEGY_DOCUMENT()
                    {
                        DOCUMENT_ID = mgrDocument.ID,
                        MANAGERSTRATEGY_ID = 7273

                    };
                    mgrStrategyDocument = mgrStrategyDocumentService.Create(mgrStrategyDocument1);
                }
                else
                {
                    throw new Exception("The file is missing.");
                }

            //return  response = Request.CreateResponse(HttpStatusCode.OK, mgrStrategyDocument);
        }
            return null;

        }

0 个答案:

没有答案