i have a backend url where i can load an xml. The whole thing works fine with jQuery.ajax. But with me the file is only displayed in the download manager when he has finished loading it. That means the user presses the button and only after x minutes the file is there, but without any download bars.
The whole thing looks like this:
But I would like to use it as it is usual. I want the file to be displayed in the download manager and show a progress.
Example:
Is there any setting for this in Ajax or is this a backend task?
My ajax Settings:
cache: false,
type: "POST",
xhrFields:{
responseType: "blob"
},
data: oData
Related
I am attempting to use the built in function sendFile() provided by Yii2 to allow users to download files. This will not, however, actually download the file.
Below is my ajax code
$.ajax({
url: 'https://'+window.location.hostname+'/download',
dataType: "json",
type: 'POST',
data: {name: name},
})
Server side code
$filename = "test.txt";
$path = Yii::getAlias('#webroot')."/uploads/test.txt";
Yii::$app->response->sendFile($path, $filename)->send();
//I've also tried variations of the file path and name. E.G:
$filename = "test.txt";
$path = Yii::getAlias('#webroot')."/uploads";
The code provided above is what I am currently using to download the file. When a user clicks on a download icon, an Ajax call is made to the action containing the logic above, thus sending that file to the user's browser.
When the Ajax call is made, the server returns 200 but doesn't actually download the file. Instead, in the response is the content of the file being requested. For instance, if the user requests a file containing the text 'Hello there!', when the Ajax call is finished, nothing will be downloaded but the server response (as seen through FireFox dev tools) shows 'Hello there!'.
Is there any reason why the file itself isn't downloading?
If I just navigate to the url (lets say its localhost/downloadFile) in another tab, the action is called, the download dialogue opens, and I can download the file.
First thing you have to return the statement, and there isnt any use of calling send() after the sendFile() if you are returning it from the controller action, just keep it like below
return Yii::$app->response->sendFile($path, $filename);
Ajax isnt for file downloads you should either create a popup window or simply use
window.location.assign('https://'+window.location.hostname+'/download/'+name);
And you will see that the page wont change that you are currently on and the file download dialog will be triggered.
I have a complex table generated via javascript that I'd like to save as PDF on the server. I've looked around at the pdf generation libraries and they all seem to be limited in terms of style, fonts, etc (that's what I meant by 'complex'). The table can be download client side as PDF or printed.
Let's say my function that generates the form to be printed is reportBody(data); - is there a way I can use AJAX to send the document as PDF to a php file that will save it server-side instead of downloading it client-side? The reportBody(data) is a collection of other variables, function calls etc.
So basically the question is - since we can generate a PDF file client-side, can we POST it (the pdf) via ajax to the server?
Short answer is Yes. Your provided information is still limited as it's not clear what is ran in the reporBody(data) but most PDF libraries on client side are able to give you the PDF file as base64 encoded data in form of a string.
You can then simply send that string to the server and save that as a PDF file.
A simple implementation will be something like this:
// I have used jQuery for convenience but you can use any lib or Vanilla JS
var saveData = $.ajax({
type: 'POST',
url: "url-to-the-php-script",
data: { pdfData: 'base64StringDataHere'},
dataType: "JSON",
success: function(resultData) { alert("Save Complete") }
});
Then on server side, do something like this:
$pdfData= $_POST['pdfData'];
file_put_contents('filename.pdf', base64_decode($pdfData));
Yes, there are a lot of ways. One would be, if you have html code in your reportBody(data); you could:
Send the html to a php file via ajax
in this php you could use https://wkhtmltopdf.org/ to generate a pdf file
In the client side you could point to that pdf generated
I am using the jQuery media plugin to display HTML and PDF documents on my webpage. The plugin will load any externally hosted PDF/HTML with no issues. However, when I try to provide a URL to my application which returns the file content, it never attempts to fetch the URL.
I have tried a relative URL path (/ajax/...) and a full URL path (protocol, port & all) to the app view.
I have tested the URL I want the application to call by providing the URL to the browser and it properly returns the PDF document.
Anyone have an idea to force the plugin to fetch the URL I am providing?
So after rewriting the jquery.media plugin due to its complex nature, Greg and I found the solution. Effectively, the extension type must be specified in the url. For example the url '/mypdfs/my.pdf' would work but the url 'mypdfs/123' will not because jquery.media cannot determine the file type. A way to get around this is to make an ajax HEAD request and get the content type and then pass the appropriate extension type as an option to the media call.
$.ajax({
type: "HEAD",
async: true,
url: "http://myurl.com/file",
success: function(message, text, response){
var contentType = response.getResponseHeader('Content-Type');
// Map content types to extension type
$('.media').media({type: extensionType});
}
});
Also, Malsup's library seems to be unmaintained. We did a rewrite of the library which can currently be found here. We will be adding mapping Content-Type to file extensions as time permits so that this can be more flexibly implemented. Feel free to make a pull request.
I develop a javascript application which display data from xml with charts and lists.
For now I put some sample files onto the server's directory that I load with :
$.ajax({ type: 'GET', url: 'data/default.xml', dataType: 'xml', ...})
The Xml files can be very heavy so when one of them is loaded I put the data in an IndexedDB.
In a second time I would like to let the visitor loads its own xml file by giving the filepath of the xml (f.i. : /home/user/sample.xml). I do not want to upload this file onto the server because I do not need it and it could be too big. But I do want to load those data in the IndexedDB and let the app displays data without any call to the server.
I do not know if browsers could work this way?
If they could, how can I do such a trick?
You can't use Ajax to get data from a file on the client system, but in sufficiently modern browsers you can use the File API. MDN has a guide to the File API that is friendlier then the specification.
Ok, this is killing me. I'm loading an external page with jquery:
var response = $.ajax({
url: "example.php",
dataType: "html",
async: false,
});
$("#content2").empty().prepend(response.responseText);
This part is working perfectly.
Now I know that if I I want to interact with the new content I would have to bind any new elements to an action for it to work...
$(".example").live("click",function(event){
//do something
}):
BUT, the problem is that the content that I am loading contains several .js files that contains LOTS of form validation.
Is there a way to just bind all of the new content to these external .js files?
Including JavaScript via AJAX requires those scripts to be eval'd. See $.getScript for a better way to do this.
As far as your example AJAX request is concerned, be wary of making synchronous requests as they will freeze your browser for the duration of the remote request. Instead, use asynchronous requests and handle their responses via callbacks
$.ajax({
url: "example.php",
dataType: "html",
success: function(response) {
$("#content2").html(response.responseText);
}
});