Mvc ajax call return download file






















Now, let us see the complete example as below which will give you more clarity about these steps. Complete example For the complete example, I have prepared and uploaded a file which contains all the code.

Second, call an ajax method on button click. Return a file from the action method First, create a GridView and set data to it. Third, use a byte array to store the result into it. Fourth, return a file as ActionResult. Create a blob for excel file and make it auto downloadable using jQuery First, create a blob object of excel on ajax success. Second, create an object URL and make it downloadable. This is already covered in "success" of the above first image. Display a loader while processing to generate excel file and download it using jQuery First, start a loader in beforeSend of ajax call.

Second, stop loader in complete. Output First, the button will be enabled before clicking it. Second, after button click and during processing, it will be disabled and text will be "Loading Third, after process completion, button will come back to the original state and excel file will be downloaded.

Show 4 more comments. Equals Constants. Combine Path. Message ; return Content e. GetExtension path. Raw Localizer["Report has been exported successfully"]. ReadAllBytes file1. FullName ; return File fileBytes, System. Dilshad Dilshad 1 1 1 bronze badge. A big block of code thrown up on the web without explanation doesn't help anyone. You should explain your approach and why your answer is better or different from the other answers.

AllowGet ; if sDocument. Contains ". Mohanavelu K Mohanavelu K 69 1 1 silver badge 3 3 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Who is building clouds for the independent developer? Word document, Excel file and PDF file are stored in a folder named Files inside the project directory. HTML Markup. Word document, Excel file and PDF file.

You will need to import the following namespaces. Imports System. The WebMethod accepts the name of the File to be downloaded as parameter. Finally, the Byte Array is converted into Base64 string and returned. ToBase64String bytes, 0, bytes. Length ;. Return Convert. End Function.

When any of the three Button is clicked, the DownloadFile JavaScript function is called and the name of the File is passed to it. URL window. Config Setting. You will need to set the maxJsonLength property through Web. Config configuration using the system. This is required as the size of the JSON response exceeds the default value when Files in Base64 string format are downloaded.



0コメント

  • 1000 / 1000