Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Passing Id with Excel file upload #11166


User avatar
0
KarakTheWise created

API 9.2.0 Angular UI

Greetings,

I'm using the code from the user Angular component as a template to import data from an Excel spreadsheet. The importing process is working very well. What I'm needing is a solution to get an eventId to the server. I need that id when I import the data into the db.  So far I've tried using params in the HttpClient post() as such below:

<br>

this._httpClient
    //.post<any>(this.uploadUrl, formData)
    .post<any>(this.uploadUrl, formData, {params: {'guestEventId': this.guestEventId.toString()}})
    .pipe(finalize(() => this.excelFileUpload.clear()))
    .subscribe(response => {

<br> The Url is appended as expected a https://localhost:44301/GuestEvent/ImportAttendeesFromExcel?guestEventId=21 like this. But the GuestEventController does not know how to deal with the appended parameters. I don't have to do it this way if you have another solution? Or if you could give me some advice as to how to deal with the 'new' Url so I can use and then how to grab the parameters once I get to my method on the server: <br>

[HttpPost]
        public async Task<JsonResult> ImportAttendeesFromExcel()
        {
            try
            {
                var eventAttendeeFile = Request.Form.Files.First();

<br> The above is a snippet from the controller code that is catching the JsonResult. Any advice would be awesome!

Thanks,

Scott


1 Answer(s)
  • User Avatar
    0
    KarakTheWise created

    I found a solid solution and thought I'd post it here:

    In my .ts file I added a formData.append()

    <br>

    uploadExcel(data: { files: File }): void {
        const formData: FormData = new FormData();    const file = data.files[0];
        formData.append('file', file, file.name);
        formData.append('guestEventId', this.guestEventId.toString());
    

    <br> Then on the server, I added the following roughed in code <br>

    [HttpPost]
    public async Task<JsonResult> ImportAttendeesFromExcel()
    {
    long currentGuestEventId = 0;
                try
                {
    
                    var eventAttendeeFile = Request.Form.Files.First();
                    var hasGuestEventId = Request.Form.ContainsKey("guestEventId");
                    _logger.Warn("Has GuestEventId: " + hasGuestEventId);
                    var formKeys = Request.Form.Keys;
                    foreach (var formKey in Request.Form)
                    {
                            var guestEventId = formKey.Value;
                            _logger.Warn("GuestEventId: " + guestEventId);
    
    
                        currentGuestEventId = long.Parse(guestEventId);
                        _logger.Warn("GuestEventId Int64: " + currentGuestEventId);
    
    
                    }
    
                    if (eventAttendeeFile == null)
                    {
                        throw new UserFriendlyException("You must submit a file");
                    }
    

    <br> I have some clean up to do but it is working well. Just thought I'd share.