I have a <img download> tag in my page. Every time a user clicks on it, file will download.
I can track number of clicks on this <img> tag and store in db for track number of downloads.
But what if user cancel the download file prompt (Select location for downloading file),
How can I track whether user is actually downloading file or not?
You need to use a proxy for the file. Say something like this PHP file:
<?php
$file = $_GET["file"]; // Get the file.
header("Content-disposition: attachment"); // And other headers for the file.
echo file_get_contents($file); // Start the file.
// Update the counter.
file_put_contents(intval(file_get_contents("count.txt")) + 1, "count.txt");
?>
On a good note, this // Update code executes only when the request is sent, and file is downloaded fully. Correct me if I am wrong.
If the request is sent and the code is executed, sorry, Google Chrome and Firefox starts the download of the file when the Save dialog box is there, which means, when the Save dialog box is open, the file has already started downloading and when cancel is clicked, the user doesn't get the file, but the server already receives the request, so it is like downloading a file and deleting it.
Note: Before some genius comes and says, this is vulnerable, file-name is missing, etc., let me tell you well ahead, this is just an idea to help the OP.
Related
1) situation:
On some page the user can use an file upload form to upload a text (or excel) file. After successful upload the user should be redirected to a page where he can do whatever with his file.
2) My controller class:
...
if (!$this->upload->do_upload('fileupload')) {
echo "error file upload not successfull";
}else {
redirect('import/filepreview/');
}
3) what happens
First nothing appears to happen. The file has been uploaded but the page doesn't change.
While using firebug extension it appears there has been sent a Header containing a get request to the proper target url and all the expected content is in the answer. But not displaying on site.
I actually don't know what I am doing wrong.
Solved the problem but only using a workaround.
Finally I used javascript redirection
window.location.replace("http://new.../x_id");
with an x_id which the servers answer to the file upload.
So everything works fine for me. But the original question, why CI's redirect does not work is still open for me.
I want to execute two operations in one javaScript function
force download of a csv file
send an ajax request to mark records in table that records were downloaded.
Since both of these operations will present the user with prompts and dialogue boxes, I want to delay execution of the 2nd event until the first event is complete.
When the user opens the page, they are presented with a list of current records. They check records they wish to process. The checked records are then packaged and the user is presented with the download "Save" or "Open" box. when that operation is complete then the ajax request should execute.
My thought was I could detect when "a" was removed from the document. But I'm not sure how I would do that.
Here is my code for the download
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob(exportArray, {type: 'text/csv'}));
a.download = 'jobCostHandOff.csv';
// Append anchor to body.
document.body.appendChild(a)
a.click();
// Remove anchor from body
document.body.removeChild(a)
I have implemented such a mechanism in a web application with the following workflow:
the user selects a file on their computer
they configure options of the app
they click the submit button
a loader starts animating client-side
the browser send the entire form to the server for processing
the processing (which is actually about converting a guitar score to a tablature) yields a text/plain file, which is stored on the server (but not sent yet): a small JSON with the link to the generated file is sent back. That JSON data represents either success or error. In the case of error, the message is displayed to the user. In the case of success, the status field is set to OK and the payload contains the link to the file.
the JS code handling the AJAX call, upon success, triggers the download.
the browser presents the user with the usual (e.g. in Firefox) "open or save file" dialog
the web app detects the opening of the dialog and hides the loader.
It seems pretty similar to your need.
The gist of the solution is this:
triggering the file download is done by setting the src attribute of an iframe hidden in the page
the server sends a cookie in response to the request for the file to be downloaded
a JS code on a timer inspects the cookies of the document (which, very fortunately, includes the cookies to the iframe) and upon seeing "the Holy cookie", knows for sure that the file download has started, which is the only event the code can grab.
Quickly looking through the PHP code of my web app, I see this:
// This cookie is for the jQuery.fileDownload plugin
header('Set-Cookie: fileDownload=true; path=/');
You have the name of the JS code that does the last bullet point item in the code comment :-) Another reason among the myriad of reasons why I write commented code...
HTH.
I have a button that lets users download a file.
The file is returned from the server in the Response (binaries set as attachment), and I am letting the browser handle the file download from there on out.
Im doing the following on button click:
var fileUrl = 'mysite.com?id=12345';
document.location.href = fileUrl;
This will load the file, however, it can take a couple of seconds. I would like to show a preloader but then ofcourse I have to know when the file has been downloaded completed. Since I'm staying on the same page, is there a method or callback that tells if the 'new' location is loaded and thus I can hide the preloader?
Thanks!
Assuming you can download the file twice, you could load the file in a hidden iframe and detect when the iframe is done loading. Then, the file is already cached and should download quickly.
Is it possible to check if there's an active file upload via PHP?
I guess this would have to be done using JS if there was any way at all but I'm unsure of where to start. From Googling, I think ignore_user_abort() may be the function I need as that way I wouldn't have to worry about any checks, but it would be interesting to find out if JS can detect an active PHP upload
PHP is a server side language so once the php code is executed there is either no new file uploaded or there is a new file uploaded, it's never executed during a file upload because the file upload is attached to the request to run the php.
Unless you handle the execution of the submit form by javascript there is no way to track the uploading of a file because the moment you press submit in a normal html field the javascript on the page stops executing because the browser is working on the new page that is being loaded.
I am building a Discussion Forum as part of a bigger application I am building, the forum is just 1 section of the Application.
For my TextArea fields when posting a new Topic or a Post Reply, I have decided that nothing is as good as the PageDown Markdown Library. It is the same one that StackOverflow uses on all their sites and it works better than many of it's competitors.
The way the library ships though, I am not happy with the default Insert Image functionality. You hit the button to insert an image and it allows you to enter a URL for an Image and then it inserts the proper MarkDown syntax to show the linked image.
This just won't cut it. I need the functionality that you see on StackOverflow! Very similar anyways.
I need it to show a Dialog when you click the Insert Image button, like it does now, but instead of just an input field for a Image URL, it will have 2 filed options...
Upload image from your computer
Insert an Image URL and it will then DOWNLOAD the image from that URL and insert it into the post just as if you had uploaded it from your computer. This is important to not confuse this step. IT should not simply insert the Image linking it to the original Image URL. Instead it will take that URL and download/upload the Image to the same server that the upload from computer option does and then it will insert the NEW Image URL pointing to the newly uploaded image!
Based on some simple HTML like below for a Dialog window with a filed for my Upload from Computer functionality, which I already have working. I need to come up with some JavaScript and PHP that will download/save a remote image to my upload folder on my server when a button is clicked using only the URL that will be inside the URL text input field.
So it will need to do a few things...
Fetch and save an image file to my uploads folder using PHP when the only thing that the PHP function will receive is a URL of the image which could be on the same server or most likely a remote server.
After successfully saving/uploading an image from the URL, the PHP function will return a JSON string with the status/error and if successful then it will also return the actual URL and filename of where the new image is saved on the local server. The JavaScript/AJAX script will receive this JSON response and insert the Markdown syntax for the image into the PageDown editor.
The PHP function will need to ensure that the URL that it is trying to save/download is a valid image file and not some malicious file! Also not simply just some file of the wrong filetype like a non-image file unless we are allowing the file type.
It will be part of a module installed on many dinosaur servers so it needs to work on as many servers as possible too!
From the web
From your computer
I would be greatful of any help, tips, code snippets or anything to help with this. At this stage I really just need to build a nie PHP function that will upload images from a remote URL and also ensure that the URL passed in is a real image file or even better that it is in the allowed file types array!
A couple years ago I had started this but have now lost it and I am starting over and don't remeber much about how I went about doing it then.
The easiest way to download a file from a remote server would be to use copy (http://php.net/manual/en/function.copy.php):
copy('http://someurl.com/image.png', '/var/www/uploads/image.png');
As this function returns a bool, it is easy to determine whether the operation was successful and create a JSON response.
To verify that the file is an actual image, there is unfortunately no way that is 100% sure. It is probably enough to check the mimetype though. You can use finfo for that (http://php.net/manual/en/function.finfo-file.php):
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $filename);
finfo_close($finfo);
For a gif, this would return image/gif for example. You will have to hardcode a list of all mimetypes you want to allow.