Override Download

Hello,
I am trying to override the download event of the file.

For my scenario when a user clicks the download button from the Viewer I need to do the following 2 things before allowing them to actually download the file.

1. I need to make a call to our server to log the fact that a user has downloaded the specified file.

2. I need to check to see if the file being sent back to the client for download is a pdf. If the file is a pdf I need to get that file so that I can modify it before the user downloads it.

I think using the following:

var containerElement = $("#DocumentViewer");
containerElement.groupdocsViewer("on", "downloadButtonClick.groupdocs",
function (e, zoom) {
});

will allow me to be able to accomplish the first item. But I am unsure how I would take care of the second one...

Thanks.

Robert

Hello Robert,


Thank you for your request. Yes, you are right - you should use the JavaScript event of the Viewer widget “downloadButtonClick.groupdocs” and then in the function simply send ajax request to the server side controller which will check file extension System.IO.Path.GetExtension(FileName) and then if all done return the file to the ajax with such code:
Response.ContentType = “application/octet-stream”;
Response.AppendHeader(“Content-Disposition”,“attachment; filename=logfile.txt”);
Response.TransmitFile( Server.MapPath("~/logfile.txt") );
Response.End();
return Response;

Best regards.

Not quite the information I was looking for. Checking the name of the file to see if its a pdf and sending it to the client to download is the easy part.


Lets say I have a file named “financials.doc”. I use the group docs viewer to display this file. I have placed a watermark on the file, told the viewer to save as pdf if possible,

The viewer opens up financials.doc and creates a lot of files in the temp folder used for storage. When the user clicks the download button they are returned a pdf instead of the original .doc file.

What I need to know is where in the javascript function can I find out the location of the newly created .pdf file that the viewer is trying to send to the client. I have used console.log(e) to try and find this myself but could not find the information.

I have used console.log(e) in the javascript below to find out where the newly created pdf file is but have not been able to find it.

var containerElement = $("#DocumentViewer");
containerElement.groupdocsViewer(“on”, “downloadButtonClick.groupdocs”,
function (e, zoom) {
console.log(e); // Need to get the location of the newly created
// temporary file created to send to the clientInformation
});

Hi again,


Thank you for the clarification. Unfortunately the pdf which user download in such case doesn’t created physically as a file on the server file system. The Viewer creates this pdf as a stream “on a fly”. Since that you will need to save it locally on the server first.

Best regards.

How would I save it locally to the server first?


I have tried

file.name = “test.doc”;

// Create a DocumentCache
DocumentCache test = new Groupdocs.Web.UI.DocumentCache(ConfigurationManager.AppSettings[“DocumentViewerLicense”].ToString(),
Server.MapPath("~/App_Data"), null);

// Save the file locally (our files our actually on another server and we usually stream them in)
System.IO.File.Copy(file.Path, System.IO.Path.Combine(Server.MapPath("~/App_Data"), file.FileName), true);

test.GeneratePdfVersion(file.FileName);

This does create a pdf version of the file but I cant find out where exactly it created it.

I have also tried the following in a controller:


// set the working directory
var workingdirectory = System.IO.Path.GetDirectoryName(fd.Path);
// convert the file to a pdf (returns bytes)
var convertedfile = new Groupdocs.Engine.Viewing.HtmlViewingService(workingdirectory).GetPdfFile(fd.FileName, 0, true);
// convert the file to end with pdf
var convertedfilename = System.IO.Path.GetFileNameWithoutExtension(fd.FileName) + “.” + “pdf”;
// set the location to copy the new temp file
var newfilepathwithname = System.IO.Path.Combine(System.IO.Path.Combine(“TempPathToFile”, convertedfilename);
// write the bytes to a file
System.IO.File.WriteAllBytes(newfilepathwithname, convertedfile);
// set the new file path and name
fd.Path = newfilepathwithname;
fd.FileName = convertedfilename;

Response.ContentType = “application/octet-stream”;
Response.AppendHeader(“Content-Disposition”, “attachment; filename=” + convertedfilename + “”);
return File(new FileStream(fd.Path, FileMode.Open), “text/plain”, convertedfilename);

And then placed javascript on the viewer page:

$(document).ready(function () {
var containerElement = $("#DocumentViewer");
containerElement.groupdocsViewer(“on”, “downloadButtonClick.groupdocs”,

function (e, zoom) {
window.location = ‘PointerToMyController’;
e.stopImmediatePropagation(); // none of this seems to work either
e.preventDefault();
e.stopPropagation();
return false;
});
});

The javascript runs, the page then hits my controller but the file that I created using the GetPdfFile method is not returned (even though when debugging it goes all the way to the return File line). It just returns the file from the Viewer. The e.preventDefault() and others dont seem to stop the click event either…

Hi,


So, you need a “intermediary” between a client call and return the file. for this:
1. The client call should go not to the GetFileHandler but to your own custom written handler, for example MyOwnFileHandler. This handler must be registered in the web.config instead of default Viewer handler, it means that you should put instead of this handler:


with something like this


2. In your handler (controller) you have to collect all the input parameters in the DTO with name “GetFileParameters” and namespace will be Groupdocs.Web.UI.DataTransferObjects

3. Instantiate Groupdocs.Web.UI.Core.CoreHandler object and call "GetFile"method for him
and in the response you will get resulting array which will be the content of the file.


Code example attached. Please note that the code example is just an example and you should update it as you need.

Best regards.

Thanks! Took a little bit of tweaking but that is exactly what I was looking for.


Robert

Hi Robert,


Glad to hear that.

Best regards.