+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Dynamic Module - PDF download is not working

Dynamic Module - PDF download is not working

6 posts, 0 answered
  1. Pratik
    Pratik avatar
    4 posts
    Registered:
    06 Feb 2012
    12 Aug 2013
    Link to this post
    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
  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    12 Aug 2013 in reply to Pratik
    Link to this post
    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
  3. Pratik
    Pratik avatar
    4 posts
    Registered:
    06 Feb 2012
    13 Aug 2013 in reply to SelAromDotNet
    Link to this post
    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
  4. Ivan D. Dimitrov
    Ivan D. Dimitrov avatar
    310 posts
    Registered:
    26 Mar 2015
    15 Aug 2013
    Link to this post
    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
  5. Pratik
    Pratik avatar
    4 posts
    Registered:
    06 Feb 2012
    16 Aug 2013 in reply to Ivan D. Dimitrov
    Link to this post
    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
  6. Ivan D. Dimitrov
    Ivan D. Dimitrov avatar
    310 posts
    Registered:
    26 Mar 2015
    20 Aug 2013
    Link to this post
    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
6 posts, 0 answered