Dynamic Module - PDF download is not working

Posted by Community Admin on 04-Aug-2018 13:01

Dynamic Module - PDF download is not working

All Replies

Posted by Community Admin on 12-Aug-2013 00:00
Hi Team,

I am doing one small thing - Get the attached .pdf file from one of the content item of my dynamic module and give that file as a download to users.

when i open the downloaded file, it always says "not supported type or file has been damaged...". 

I attached snapshot of the error.

Here is the code what i am using for getting the .pdf, loading into stream & writing it on the httpresponse. 

protected void BtnInvlidResource_Click(object sender, EventArgs e)
           
            var researchArticleRedirectUrl = Session["ResearchArticleRedirectUrl"];
            var researchArticleId = Session["researchArticleId"];
            //var articleContext = App.WorkWith().Document(new Guid(researchArticleId.ToString())).GetLive().Get();
            DynamicContent researchArticle = RetrieveResearchArticleById(new Guid(researchArticleId.ToString()));
            var documentUrl = string.Empty;
            var docManager = LibrariesManager.GetManager();
            if (docManager != null)
               
                var contentLinks = (ContentLink[])researchArticle.GetValue("PDFDownload");
                var documentLink = contentLinks.FirstOrDefault();
                if (documentLink != null && documentLink.ChildItemAdditionalInfo != null)
                   
                    var artcileStream = docManager.Download(documentLink.ChildItemId);

                    var doc = docManager.GetDocument(documentLink.ChildItemId);
                    docManager.Provider.SuppressSecurityChecks = true;
                    var req = new WebClient();
                    Response.Clear();
                    Response.ClearHeaders();
                    Response.ClearContent();
                    Response.Buffer = true;
                    bool isClientConnected = Response.IsClientConnected;
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + doc.ItemDefaultUrl + ".pdf");
                    Response.AddHeader("Content-Length", artcileStream.Length.ToString(CultureInfo.InvariantCulture));
                    Response.AddHeader("Content-Type", "Application/pdf");
                    Response.AddHeader("Pragma", "public");
                    Response.AddHeader("Accept-Ranges", "bytes");
                    Response.ContentType = doc.MimeType;
                    try
                       
                        //BinaryReader reader = new BinaryReader(artcileStream);
                        byte[] data = new byte[(int)artcileStream.Length];

                        Response.BinaryWrite(data);

                        //byte[] data = req.DownloadData(researchArticleRedirectUrl.ToString());

                        //Response.OutputStream.Write(data, 0, data.Length);
                       
                    catch (Exception)
                       
                        throw;
                       
                    finally
                       
                        //Response.Flush(); not neededed as response buffer is set to true
                        Response.End();
                       
                   
               
           

        protected void Page_Load(object sender, EventArgs e)
           

           

        private static DynamicContent RetrieveResearchArticleById(Guid researchArticleId)
           
            var dynamicModuleManager = DynamicModuleManager.GetManager();
            var researchArticleType =
                    TypeResolutionService.ResolveType(
                        "Telerik.Sitefinity.DynamicTypes.Model.ResearchArticles.ResearchArticle");

            // This is how we get the researchArticle item by ID
            var researchArticleItem = dynamicModuleManager.GetDataItem(researchArticleType, researchArticleId);
            return researchArticleItem;
           

Can you please suggest what i am missing here.

Regards,
Palak
Posted by Community Admin on 12-Aug-2013 00:00
I'm not all that familiar with writing binary files directly to the output stream, but I wonder: could you accomplish essentially your desired goal by simply redirecting the request to the Url for the Document? It should respond with the appropriate response and allow the user to download or view the file.

I hope that this is helpful, let me know if this approach isn't an option and I'll try my best to help
Posted by Community Admin on 13-Aug-2013 00:00
Hi SelAromdotNet,

The problem is we are trying to achieve two things at the same time  :
1 ) we want to navigate user back to some other page
2 ) we want to give one file download to user

Currently we are opening document in a new window from javascript, and redirect existing browser window to some other page.

Due to pop-up blocker in the browser, user must open it for the website domain. To get rid of this issue, we are trying to give file download to user in same browser window & once they download the file, we navigate them to some other window.

there are some methods to write response i.e. Binarywrite, outputstream & webclient.downloaddata. i used many times. i think there is something with dynamic module and the way it generates URI. If you know more about it.

Thanks,
Palak
Posted by Community Admin on 15-Aug-2013 00:00
Hi Pratik,

In my view the best way to handle your situation is by using Telerik RadControls. Have a look at this sample. Basically you can use RadGrid and all its functionalities and the only thing you need to add is a redirect after the pdf download.


Regards,
Ivan Dimitrov
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Posted by Community Admin on 16-Aug-2013 00:00
Hi Ivan,

I got the solution for downloading issue. If the file was larger in size, it was getting timed out during reading.
R/w in chunks through Response.OutputStream.Write() worked perfectly for large files.

Regarding RadControls, our solution is different in design, we just fetch required file from database & give download to users, we then need to redirect users to some other page of the site.

Do you have any idea how to achieve programmatically to redirect users after response.close() ?

Regards,
Palak
Posted by Community Admin on 20-Aug-2013 00:00
Hello Patrik,

You can try the Responce.Redirect method. I tested this simple code and it seems to work fine.
private void Page_Load(object sender, EventArgs e)
 
     // Check whether the browser remains
     // connected to the server.
     if (Response.IsClientConnected)
     
         // If still connected, redirect
         // to another page.
         Response.Redirect("URL that I redirect to", false);
     
     else
     
         // If the browser is not connected
         // stop all response processing.
         Response.End();
     
 



Regards,
Ivan D.Dimitrov
Telerik
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
This thread is closed