locked
How I download iframe content ( Image/PDF) RRS feed

  • Question

  • User-582711651 posted

    Hi Friends, 

    Please help me, I am using three different iframes in my asp web form and I can able to see JPG/PDF files from my application folder. My request is how I download this content when clicking the buttons by using VB codings.

    this is my iframes.

    <td>
    <iframe ID="ifrm_dcbform" runat="server" name="I5" scrolling="yes" style="height:486px; width:360px"></iframe>
    </td>
    <td>
    <iframe ID="ifrm_uid" runat="server" name="I3" scrolling="yes" style="height:486px; width:360px"></iframe>
    </td>
    <td><iframe ID="ifrm_rcvc" runat="server" name="I4" scrolling="yes" style="height:486px; width:360px"></iframe>
    </td>

    This is my Buttons

    <td>
    <asp:Button ID="Btn_dcb_form" runat="server"  Text="Form Download" />
    </td>
    <td>
    <asp:Button ID="Btn_dcb_uid" runat="server"  Text="UI Download" />
    </td>
    <td>
    <asp:Button ID="Btn_dcb_rcvc" runat="server"  Text="RCV Download" />
    </td>

    Thanks in advance. 

    Sunday, August 5, 2018 5:20 PM

All replies

  • User-893317190 posted

    Hi Ayyappan,

    You need to use response.WriteFile method to write your wanted file into response and  modify your response header  in your button click event.
    More information for response.WriteFile

    https://msdn.microsoft.com/en-us/library/dyfzssz9(v=vs.110).aspx

    Below is my code.

    <form id="form1" runat="server">
        <iframe ID="ifrm_dcbform" runat="server" name="I5" scrolling="yes" style="height:300px; width:360px"
            src="~/File/print.pdf"
            ></iframe>
        
            <asp:Button ID="Button1" runat="server" Text="download" OnClick="Button1_Click" />
    

    And my code behind. "target.pdf" is the new name of your downloaded file.

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
            Response.AddHeader("Content-Disposition", "attachment;filename=target.pdf")
    
    
            Response.WriteFile(Server.MapPath(ifrm_dcbform.Src))
    
    
        End Sub 
    

    Best regards ,

    Ackerly Xu

    Monday, August 6, 2018 7:58 AM
  • User-582711651 posted

    Hi Ackerly, 

    No, I am getting an error, and for your information in the <g class="gr_ gr_302 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="302" data-gr-id="302">iframe</g>, I'm accessing both jpg and pdf file.

    This is error message: 

    Error	636	'src' is not a member of 'System.Web.UI.HtmlControls.HtmlGenericControl'.	 

    Thanks,

    Monday, August 6, 2018 8:57 AM
  • User-893317190 posted

    Hi Ayyappan,

    Perhaps  you are not using asp.net framework 4.5 or  higher version.
    If so , the control doesn’t have a src attribute , you could try to use

    ifrm_dcbform.Attributes.Item("src")
    Protected Sub Button1_Click(sender As Object, e As EventArgs)
            Response.AddHeader("Content-Disposition", "attachment;filename=target.pdf")
    
    
             Dim path As String = ifrm_dcbform.Attributes.Item("src")
    
            Response.WriteFile(Server.MapPath(path))
        End Sub 
    

    If it still doesn’t work , you could write the path directly , for example

    Dim path As String =”/file/pdf/yourpdffilename.pdf”

    Best regards ,

    Ackerly Xu

    Monday, August 6, 2018 10:00 AM
  • User-582711651 posted

    Hi Ackerly Xu, 

    yes, it works, but only one thing, I have to access both JPG and PDF in <g class="gr_ gr_231 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="231" data-gr-id="231">iframe</g>, the user may not know which file has shown, how I classify file type based on "SRC" extensions whether PFF or JPG

    See here, 

    Response.AddHeader("Content-Disposition", "attachment;filename=target.jpg")
    
    or 
    
    Response.AddHeader("Content-Disposition", "attachment;filename=target.pdf")

    Hope you understand.

    with regards, 

    Monday, August 6, 2018 10:21 AM
  • User-893317190 posted

    Hi ayyappan,

    You could use Path class's GetExtension method to help you. This method could get the extension of a string path with "." as its start.

    For example , Path.GetExtension("~/file/print.pdf") will get the result ".pdf".

    So ,you could write your code like below.

     Protected Sub Button1_Click(sender As Object, e As EventArgs)
    
    
    
            Dim pat As String = ifrm_dcbform.Attributes.Item("src")
            Dim ext = Path.GetExtension(pat)
    
            Response.AddHeader("Content-Disposition", "attachment;filename=target" + ext)
            Response.WriteFile(Server.MapPath(pat))
        End Sub

    Please don't name the value of your iframe's src attribute  as "path"  or it will  conflict with  the Path class.

    Best regards,

    Ackerly Xu

    Monday, August 6, 2018 11:51 AM