Is there a SaveFileDialog in .Net Core 2.2?

2.4k views Asked by At

I am Developing an app with a feature that downloads some records and saves those as a text file. It's working if I put a static location for the resulting file. I want to let the user decide where they want to save this file. Is there a SaveFileDialog in Asp.Net Core 2.2 MVC?

My Download action in my controller is below:

 public async Task<IActionResult> DownloadList([Bind("Id,isDownload")] BdoPE bdoPE)
        {
            UserDetails();
            string cncuser = ViewBag.DisplayName;
            var config = new CsvHelper.Configuration.Configuration();
            config.Delimiter = "\t";

            var records = new List<BdoRpt>();

            var record = _context.bdoPEs.Where(
                    c => c.DocType != null &&
                    c.isDownloaded == false &&
                    c.CompanyCode != null && 
                    c.AssignNum != null &&
                    c.ItemText != null &&
                    c.ItemText2 != null && 
                    c.isDownloaded == false &&
                    c.MarketerZ2 == cncuser).ToList();

            if (record.Count > 0)
            {
                foreach (var data in record)
                {
                    records.Add(new BdoRpt()
                    {
                        DocDateInDoc = data.DocDateInDoc,
                        DocType = data.DocType,
                        CompanyCode = data.CompanyCode,
                        PosDateInDoc = data.PosDateInDoc,
                        FiscalPeriod = data.FiscalPeriod,
                        CurrentKey = data.CurrentKey,
                        RefDocNum = data.RefDocNum,
                        DocHeadT = data.DocHeadT,
                        PosKeyInNextLine = data.PosKeyInNextLine,
                        AccMatNextLine = data.AccMatNextLine,
                        AmountDocCur = data.AmountDocCur,
                        ValDate = data.ValDate,
                        AssignNum = data.AssignNum,
                        ItemText = data.ItemText,
                        PosKeyInNextLine2 = data.PosKeyInNextLine2,
                        AccMatNextLine2 = data.AccMatNextLine2,
                        AmountDocCur2 = data.AmountDocCur2,
                        BaseDateDueCal = data.BaseDateDueCal,
                        ItemText2 = data.ItemText2,
                    });
                }

                using (var writer = new StreamWriter("C:\\file.txt")) // this is the static location
                using (var csv = new CsvWriter(writer, config))
                {
                    csv.WriteRecords(records);
                }
                recordDownloaded();
                var bdope = _context.bdoPEs.Where(
                    c => c.DocType != null &&
                    c.isDownloaded == false &&
                    c.CompanyCode != null &&
                    c.AssignNum != null &&
                    c.ItemText != null &&
                    c.ItemText2 != null &&
                    c.isDownloaded == false).ToList();

                foreach (var data in bdope)
                {
                    data.isDownloaded = true;
                }
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Lsmw));
            }
            else
            {
                NoRecordFound();
                return RedirectToAction(nameof(Lsmw));
            }
        }

In my code currently it is simply the text file on my drive C:\

2

There are 2 answers

6
Farhad Rahmanifard On

You can show a download file dialog instead:
https://stackoverflow.com/a/50334901/5137920
Please be careful not to use any library that is related to desktop development directly to avoid any compatibility issues.

0
Fran On

There is no way of displaying a save dialog directly from your application. You should return a FileResult or any derived implementation and let the browser decide what to do with your data. There is a function that can help you in this.

See this link.