How to modify web api to return message not matched compare when excel compare return false ?

ahmed salah 3,216 Reputation points
2022-04-01T23:15:56.227+00:00

I work on asp.net core 2.2 i face issue I can't

modify web api to display message "Not Matched Compare" when compare excel function return false (areIdentical == false) .

web api below return physical file as zip file

in case of compare excel return true

but I need when compare excel function return false

to display message "Not matched compare"

so How to do it please ?

what i have tried

public IActionResult ExportNormalizedRelation()
{

        var InputfilePath = System.IO.Path.Combine(GetFilesDownload, "DeliveryGenerationNormalTest_Input.xlsx");
        using (var stream = new FileStream(dbPath, FileMode.Create))
        {
            Request.Form.Files[0].CopyTo(stream);
            stream.Flush();
            stream.Close();
        }
        bool areIdentical = ex.CompareExcel(dbPath, InputfilePath, out rowCount, out error);
        if (areIdentical == true)
        {

            pathToCreate = Path.Combine(myValue2,Month,"file" + DateTime.Now.Ticks.ToString());

                Directory.CreateDirectory(pathToCreate);

                List<inputexcelnormaltest> inputexcellist = new List<inputexcelnormaltest>();
                inputexcellist = ex.ImportNormalTest(dbPath);
                List<string> tabs = new List<string>();
                var outputTemplatePath = System.IO.Path.Combine(GetFilesDownload, "DeliveryGeneration_Output.xlsx");
                List<inputexcelnormaltest> inputmodulelist = new List<inputexcelnormaltest>();



                        foreach (var m in tabs)
                        {
                            inputmodulelist.Clear();
                            inputmodulelist = inputexcellist.Where(x => (x.TabName == m && x.FileName == f)).ToList();
                            var dtimport = DatatableConversion.ToDataTable(inputmodulelist);
                            DataTable dtexport = new DataTable();

                            dtexport = _deliveryService.LoadExcelToDataTableZipData(_connectionString, dtimport);

                            ex.Export(dtexport, m, exportPath);

                        }




        }

        string zipPath = Path.Combine(myValue2,Month,"result" + DateTime.Now.Ticks.ToString() + ".zip");

        ZipFile.CreateFromDirectory(pathToCreate, zipPath);

        return PhysicalFile(zipPath, "application/zip", Path.GetFileName(zipPath));



}

expected result

if (areIdentical == false)
return "Not Matched Compare"

so How to do that ?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,227 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,348 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,026 Reputation points Microsoft Vendor
    2022-04-04T06:01:44.3+00:00

    Hi @ahmed salah ,

    web api below return physical file as zip file

    in case of compare excel return true

    but I need when compare excel function return false

    to display message "Not matched compare"

    so How to do it please ?

    In the ExportNormalizedRelation method, you could use a if-else statement to return the data.
    Like this:

        [HttpPost, DisableRequestSizeLimit]  
        [Route("ExportNormalizedRelation")]  
        public IActionResult ExportNormalizedRelation()  
        {  
            bool areIdentical =false;  
            if (areIdentical)  
            {   
                var zipPath = @"C:\Users\Administrator\Downloads\AspNetCore-3x-RCL-plugins-master.zip";  
                return File(System.IO.File.ReadAllBytes(zipPath), "application/zip", Path.GetFileName(zipPath));  
            }  
            else  
            {  
                return Ok("Not Matched Compare");  
            }  
    
        }  
    

    Then, the result as below:

    If the areIdentical is true:
    189623-image.png

    If the areIdentical is false:
    189534-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. AgaveJoe 26,161 Reputation points
    2022-04-02T11:15:35.947+00:00

    I think your main question is how to return a file and a JSON message.

    It is not possible to return a file stream and a JSON message in a single HTTP response. You need to do two requests.

    0 comments No comments