locked
Download option not working when Array of Byte is converted into PDF RRS feed

  • Question

  • User1360833881 posted

    Hi,

    I have a MVC controller in which, I am converting Array of Byte into PDF and downloading in target path, even pdf file is getting downloaded in specified path, but download option is not working i.;e pdf file not downloading in C:\Users\sayedab\Downloads, I feel I am missing something, kindly verify my code and help me in this regard. Thanks.

     public ActionResult PrintContract(string legalid)
            {
               var objApplicationInputBL = new REDF_BL();
                    PrintContract_Input objPrintContract_Input = objApplicationInputBL.PrintContract(legalid);
    		var responsePrintContract = objFinanceClient.printContract(objCredentials, objSupportContract);
     		//Call Contract Download method
                     DownloadContract(objPrintContract_Input.NID, responsePrintContract.ContractByte);
    	}
    private void DownloadContract(string nid, byte[] contractByte)
            {
                try
                {
                    string savePath = System.Web.HttpContext.Current.Server.MapPath(@"~/") + @"SavePath\" + nid + "_" + "REDF Contract_" +
                                        System.DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf";
                    System.IO.File.WriteAllBytes(savePath, contractByte);
                }
                catch (Exception ex)
                {
    
                    _redfLogs.WriteError(ex);
                }
               
            }

    Wednesday, April 29, 2020 12:26 PM

Answers

  • User1360833881 posted

    Dear jiadongm,

    Thanks for your kind response, instead of FileContentResult, FileResult is worked for me, so file downloading both in server and in browser, but its staying in the same page/view, how to redirect it to anther view, because "RedirectToAction" will not work as Action method is now "FileResult".  Thanks in advance. 

    public FileResult PrintContract(string legalid)
            {
     string savePath = System.Web.HttpContext.Current.Server.MapPath(@"~/") + @"SavePath\" + objPrintContract_Input.NID + "_" + "REDF Contract_" +
                                         System.DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf";
                     System.IO.File.WriteAllBytes(savePath, responsePrintContract.ContractByte);
                     return File(savePath, "application/force-download", Path.GetFileName(savePath));
     //return RedirectToAction("GetCustomerDetails", "Customer");
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2020 2:37 PM

All replies

  • User-17257777 posted

    Hi alibasha202,

    From your codes, it seems what you have done is just uploading the pdf file. If you want to download it, you can define a method and return as a FileContentResult.

    Refer to this link https://stackoverflow.com/a/16975819/11965297

    Hope it can help you.

    Best Regards,

    Jiadong Meng

    Thursday, April 30, 2020 6:42 AM
  • User1360833881 posted

    Dear jiadongm,

    Thanks for your kind response, instead of FileContentResult, FileResult is worked for me, so file downloading both in server and in browser, but its staying in the same page/view, how to redirect it to anther view, because "RedirectToAction" will not work as Action method is now "FileResult".  Thanks in advance. 

    public FileResult PrintContract(string legalid)
            {
     string savePath = System.Web.HttpContext.Current.Server.MapPath(@"~/") + @"SavePath\" + objPrintContract_Input.NID + "_" + "REDF Contract_" +
                                         System.DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf";
                     System.IO.File.WriteAllBytes(savePath, responsePrintContract.ContractByte);
                     return File(savePath, "application/force-download", Path.GetFileName(savePath));
     //return RedirectToAction("GetCustomerDetails", "Customer");
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 30, 2020 2:37 PM
  • User475983607 posted

    It's not possible to download a file and redirect at the same time.  Redirect then down load the file.

    Thursday, April 30, 2020 4:52 PM
  • User1360833881 posted

    Hi,

    Thanks for your reply, while understand that It's not possible to download a file and redirect at the same time. But how to Redirect then download the file, because even if I redirect to any action method, then its return would be of file type, how can I return the required view in that action method. Can you please explain me on a high level. Thanks.

    Saturday, May 2, 2020 12:37 AM
  • User-474980206 posted

    http is request / response. you can nit do what you want server side is you want a redirect and a download. you need to use javascript. the page load of the redirect have javascript do the download. the original page can have javascript do a download via a form post and target, then do a redirect. you usually see sites have the page say "download started, if you don't see it, click the download button"

    one thing you can not easily know is if the download started, then redirect. This requires javascript polling the server to check the download status.

    Saturday, May 2, 2020 12:48 AM