Home > database >  C# How To Download File From Server-Side
C# How To Download File From Server-Side

Time:02-04

im going to lose my mind and i'm a newbie. I've two controller which one is from Server-Side another one is called by jquery ajax Client-Side. I've a folder with excel files on Server-Side. I'm trying to get this folder from server-side and download from client-side to user's download folder or with any save-as dialog. Here is my Server-side Controller

[HttpGet]
        [Route("DownloadExcelFile")]
        public IActionResult DownloadExcelFile(string fileName)
        {
            try
            {
                return File(
                        fileContents: _wholesaleService.DownloadExcelFile(fileName),
                        contentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                        fileDownloadName: fileName
                        );
            }
            catch (Exception ex)
            {
                return default;
            }
        }

and my fileContents is a byte array which is like below

public byte[] DownloadExcelFile(string fileName)
        {
            string sourcePath = GetFolderPath()   fileName;
            var bytes = File.ReadAllBytesAsync(sourcePath);
            return bytes.Result;
        }

So this is my server-side. And my client-side controller is

[HttpGet]
        public async Task<IActionResult> DownloadFile(string fileName)
        {
            var response = await _httpHelper.GetFormDataAsync<IActionResult>($"WholesaleManagement/DownloadExcelFile?filename={fileName}");
            return StatusCode(200,response);
        }

This controller is triggered by an ajax call and it directly goes to server-side controller with httphelper class which is like below

public async Task<HttpResponseMessage> GetFormDataAsync<T>(string methodName, HttpStatusCode successStatusCode = HttpStatusCode.OK) where T : class
        {
            if (!urlWhiteList.Contains(methodName))
                throw new HttpRequestException(methodName);

            using (HttpClient httpClient = new HttpClient ())
            {
                var requestUri = QueryHelpers.AddQueryString(methodName, "", "");
                var response = await httpClient.GetAsync(requestUri);
                return response;
            }
        }

After all that my use case is : User will click to filename from website and i will take that filename to my controller and download this file to user's computer with a pop-up. I hope it is clear and i can give much more details if you want to. How to achieve this?

CodePudding user response:

Try this one. I spent more times before. Hope this help.

        var httpRequest = HttpContext.Current.Request;
        var filePath = string.Empty;
        HttpPostedFile postedFile = null;
        foreach (string file in httpRequest.Files)
        {
            postedFile = httpRequest.Files[file];
            filePath = HttpContext.Current.Server.MapPath("~/TempFile/"   postedFile.FileName);
            postedFile.SaveAs(filePath);
        }
  • Related