Click here to Skip to main content
15,888,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to upload an excel file and displaying the excel values in a html table. After that I will click on a button click- which will transfer the control to webapi with the values in the html table
I am using Angular and .net webapi 
So i have written a service as follows
i am trying to upload an excel file and displaying the excel values in a table.
So i have written a service as follows

import { Injectable } from '@angular/core';
import { HttpHeaders } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';



@Injectable({
  providedIn: 'root'
})
export class UploadServiceService {

  constructor(private http: HttpClient) { }
  
    url = 'http://localhost:xxxx/api/xyz/ExcelUpload'
    
  UploadExcel(formData: FormData) {
    let headers1 = new HttpHeaders();

    headers1.append('Content-Type', 'multipart/form-data');
    headers1.append('Accept', 'application/json');

    const httpOptions = { headers: headers1 };

    return this.http.post(this.url , formData, httpOptions)  
  }

}

Next I have called the service in component.ts which is as follows
getApprovals()
  {
    alert("from getapprovals");
    let formData = new FormData()
    formData.append('upload', this.arrayDisplay)
        this.service.UploadExcel(formData).subscribe(result => {
        this.message = result.toString();      
        });
    
  }
Next I am trying to access the values of the array in webapi in the following way.

if (HttpContext.Current.Request.TotalBytes > 0)
            {
                foreach (string key in HttpContext.Current.Request.Form.AllKeys)
                {
                    string value = HttpContext.Current.Request.Form[key];

                
                }
            }

But I am getting the value of HttpContext.Current.Request.Form[key] as 
"[object Object],[object Object],[object Object],[object Object]"
Actually in the array I have 4 records…. Each record has multiple values internally. 

How can I resolve this. Please help me


What I have tried:

string message ="";
           var jsonString = String.Empty;

           HttpContext.Current.Request.InputStream.Position = 0;

           //using (var inputStream = new StreamReader(HttpContext.Current.Request.InputStream))
           //{
           //    jsonString = inputStream.ReadToEnd();
           //}


           var dictionary = new Dictionary<string, object>();
           HttpContext.Current.Request.Form.CopyTo(dictionary);

           if (HttpContext.Current.Request.TotalBytes > 0)
           {
               foreach (string key in HttpContext.Current.Request.Form.AllKeys)
               {
                   string value = HttpContext.Current.Request.Form[key];


               }
           }
Posted
Updated 28-Jul-19 7:33am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900