I want to return pdf file as response with 'success or 'error' message but as of now getting pdf file and its downloading by using this code 'return File(bytes, "application/pdf", DateTime.Now.ToString("ddMMyyyy") + ".pdf");' now i want to return pdf file with success or error message. How to achieve this can anyone help me out of this
Asked
Active
Viewed 349 times
0
-
I recommend to return view model that contains file content as byte[] and message (success or false) and pass this model to view and render pdf as part of page reference to this article https://stackoverflow.com/questions/6439634/view-pdf-as-part-of-the-page with showing message on the page or on alert – Mohamed Adel Jan 11 '21 at 09:50
-
@MohamedAdel used view model but getting file response in json format with file and content details.not downloading file and this is a web api and want to download file and need to display message – Dasam Bharathi Jan 11 '21 at 10:42
-
Please add your method with view model to question – Mohamed Adel Jan 11 '21 at 10:49
-
return new Response { Data = new DocVm() { Documents = File(bytes, "application/pdf", DateTime.Now.ToString("ddMMyyyy") + ".pdf"), message = "Ok" } }; and the model is public class DocVm { public FileResult Documents { get; set; public string message{ get; set; } } – Dasam Bharathi Jan 11 '21 at 11:29
-
Just send file content as (byte[]) and handle view or download on frontend layer – Mohamed Adel Jan 11 '21 at 11:54
-
byte[] bytes = Convert.FromBase64String(responseobj.Data.ToString()); if (bytes != null && bytes.Length > 0) { return new Response { Data = new DocVm() { Documents = File(bytes, "application/pdf", DateTime.Now.ToString("ddMMyyyy") + ".pdf"), message = "Ok" } }; please have a look into this im returning like this – Dasam Bharathi Jan 11 '21 at 13:05
-
Don't return file just return bytes directly and manage it at frontend layer. return new Response { Data = new DocVm() { Documents = bytes, message = "Ok" } }; – Mohamed Adel Jan 11 '21 at 13:22