Add wait spinner for #Url.Action (download file) - ajax

Using jQueryMobile I am downloading a file using #Url.Action and a FileStreamResult in the controller class.
Controller Method
public ActionResult DownloadPDF()
{
var stream = myHandler.getFileStream("myfile.pdf");
return File(stream, "application/pdf", "myfile.pdf"));
}
Html code
<a href='#Url.Action("DownloadPDF", "Home">
<div>
<!-- HTML Content -->
</div>
</a>
Works great!
But how may I know when the download is complete, to add a wait-spinner?
I want to use the FileStreamResult as the file is not stored on the webserver (stored in db).
Is it possible to "know" when the file is downloaded by using Url.Action?

It's not possible to do this, as the page does not know anything about the request (which technically is another page entirely).
You can register that a user clicked on the link, but it is not possible to find any more information about the download request - whether it completed, cancelled or errored.

Related

Laravel response helper file() method downloading, not displaying file

I am trying to display a file stored in storage folder using laravel's response helper. Unfortunately, every time it runs, the file is DOWNLOADED, not DISPLAYED. Below is my code:
public function viewDoc($id)
{
$a = pro_doc::findorfail($id);
$path = storage_path('documents/'.$a->file_name);
if ( ! File::exists($path) ) {
abort(404);
}
return response()->file($path);
}
Im sending an id to the controller, which looks up the record and gets the file name, then attempts to open the file in a new window (this is from a link with a blank target). A new window momentarily opens, then the download begins and the window closes.
The documentation (https://laravel.com/docs/5.4/responses#file-responses) seems to say this shouldn't happen
The file method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download.
Can anyone help me make this work? Again, Im trying to get it to open in the browser window, not download. Thank you!

MVC big file download

I am using ASP.net MVC 4.0. I want to download a file after clicking a download button/a link. The problem is that the file is big and I want to show a 'wait image' while downloading. How do i do it? I am getting the file as a stream? Should I use HTPResposneMessage with web-api or FileStreamResult with MVC? The issue is I want to be notified when the download finishes.
My file is being downloaded with a code as below:
public FileStreamResult Download(Guid id)
{
.......
return this.File(cab, "application/octet-stream", id + ".cab");
}
I want to show a spinner in javascript before beginning the download. My code is as follows:
self.IsDownloading(true);
var url = '/Download/' + id;
window.location = url;
self.IsDownloading(false);
But IsDownloading(false) is being executed before downloading the full file. How to make sure that it is done after the full file is downloaded?

How to return pdf document from controller with ajax call in MVC app

I have an #Html.Action link that currently works for returning the user a pdf document from the controller. I want to be able to use an ajax call to perform the same function but I'm stuck on how or even if this can be done. I have tried several different iterations but I never get the pdf to download from the browser window. Here is what I have so far:
$('#Button1').click(function () {
var poNum = "51970";
$.ajax({
type: "GET",
data: "id= " + poNum,
url: '#Url.Action("PoReport", new { controller = "LogisticsTools"})'
});
});
I can copy the Request URL from the Headers window in Chrome Dev Tools and paste it into a new page and the pdf is downloaded. I can see the pdf code in the Response window also but it just doesn't get downloaded when the button is clicked. What am I missing?
You don't want to ajaxify this. Put the URL in an anchor tag and let the browser do the rest. If the browser doesn't recognise the document type or it is configured to force file downloads, the file will download as you expect (i.e. the user will see the download dialog). If the document is recognised, can be opened in the browser, and the browser is configured to open files, the file will open in the browser.
<a href='#Url.Action("PoReport", new { controller = "LogisticsTools"})'
title="Click to Download/Open">
Download
</a>
You can't download PDF file while using the ajax request to server. So instead of that you should use html actionlink. for example
#Html.ActionLink("Convert Data into PDF", "PDFView", null, new { #class= "btn btn-info" });
Above code will generate a link button and you can access Action Method of controller. Also you can use any technique to return PDF file from controller for example you can return PDF file using FileResult class.

Browserless web scraping of ajax page

Have tried using Selenium after reading some tutorial for web scraping ..
The aim is to web/screen scrape a page that loads the required data after an Ajax call when (this ajax call made after Initial page load)..
The second aim is to run Selenium code in the background (not opening any browser) to allow loading the page (including the Ajax call) , retrieve the Final HTML and perform required processing locally ..
the code till now is as follows (code tutorial from http://www.geekonweb.com/net/web-page-scraping-using-selenium-and-net/)
public ActionResult Index()
{
//--
//Below path should contain IEDriverServer.exe
var chrome = new ChromeDriver(#"file path");
chrome.Url = #"<url>";
chrome.Navigate();
//extract the html
//var retval = ie.ExecuteScript("return document.body.outerHTML");
string result = chrome.PageSource;
return View();
}
currently have not been able to find a way to run Selenium Silently (without GUI). kindly assist if that can be done.
Secondly kindly tell that how can Selenium be told to wait for the Ajax call to finish and then retrieve the data.
regards,
Here is a question on how to wait until an element is present. This is done to wait for the AJAX.
Here is a question on weather it's possible to run selenium headless.

Simple ajax/prototype problem

im beginning with Ajax, i have problem with including Ajax files.
Ajax code written in original page (like index.php) and placed in (head) section works fine, but when i try to place code in external file (in js folder, where is placed prototype.js file), i don't get any response, not even in Firefox Error Console.
I haven't changed Ajax code except url for calling PHP function.
edit:
calling ajax files:
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/myValidation.js"></script>
</head><body>
....
Username: <input type="text" name="uname" id='uname' />
Available?
<span id="result"></span>
Email address: <input type="text" name="email" />
...
I embaded this function call in html. Validate function is from book "PHP and Script.aculo.us Web 2.0 app interfaces"
myValidation.js
function Validate(){
var user=$('uname');
var name="uname="+user.value;
var pars=name;
new Ajax.Request(
'myValidation.php',
{
method:'post', parameters:pars, asynchronous:true, onComplete: showAvailable
}
);
}
function showAvailable(originalRequest){
var newData=originalRequest.responseText;
$('result').innerHTML=newData;
}
This example is from mentioned book
You haven't shown us your myValidation.js file, but here are the typical reasons I see when people move from inline script blocks to external files and things stop working:
They put script blocks in the external JavaScript files. You probably didn't do that, but I've seen it often enough to mention it. Your external script is pure JavaScript, so for instance it should be:
function Validate() {
// ...
}
not:
<script type='text/javascript'>
function Validate() {
// ...
}
</script>
I've seen the latter a fair bit.
They put the JavaScript file in a location that doesn't match their script tag src.
They left an opening <!-- or closing --> in the script. Important not to do that, in external JavaScript files those are syntax errors.
They're using a web server that's case sensitive and the src attribute and the file's actual name don't match.
They're using a web server sensitive to permissions and the file doesn't have the right permissions.
In the case of the last two above, it's easy to check: Just open a new tab and actually enter the URL of the JavaScript file. If you see the JavaScript, great; if not, you probably have more information.
For issues like this (and hundreds of others), there's nothing like having a decent toolset. For debugging JavaScript on browsers, there are quite a few. There's Firebug (a Firefox add-in), Chrome's and Safari's Development Tools (built into the browsers), Microsoft Visual Studio or Script Debugger for debugging with IE, etc. Firebug and Dev Tools would both tell you about broken src links, as well as any exceptions, etc.
Have you checked that those files are accessible from the HTML code? And more - have you placed you scripts in the bottom of the page - because AJAX will bind it's handlers only to existing elements?
Problem solved.
In /js/ folder i had one php file, that i put there just because of simplicity. After moving it to other location all worked. Don't know if that is rule, nut no php files in /js/ folder. Thanks T.J and Tomasz

Resources