How can I download PDF from a web page automatically

-2

Is there any way to download a PDF embed into a web page? I am currently using the control WebBrowser.ShowSaveAsDialog() (Save As) but need to do without it, automatically using C # Windows Forms

My code:

private void button1_Click_1(object sender, EventArgs e)
{
    browserPlus1.Navigate("https://www3.webiss.com.br/aracajuse/FormRelNFSe.aspx?tipo=emitirrelatorio&MostrarRel=false&idRec=verificarnfse&IdNotaEletronica=17926183&Expiration=10032016055357&Verificador=566");
    browserPlus1.ShowSaveAsDialog();

}

But in my case it does not have the URL address extension

    
asked by anonymous 24.11.2014 / 17:09

4 answers

2

I got the form below using the DDL IMPORT

 /// <summary>
    /// The URLMON library contains this function, URLDownloadToFile, which is a way
    /// to download files without user prompts.  The ExecWB( _SAVEAS ) function always
    /// prompts the user, even if _DONTPROMPTUSER parameter is specified, for "internet
    /// security reasons".  This function gets around those reasons.
    /// </summary>
    /// <param name="pCaller">Pointer to caller object (AX).</param>
    /// <param name="szURL">String of the URL.</param>
    /// <param name="szFileName">String of the destination filename/path.</param>
    /// <param name="dwReserved">[reserved].</param>
    /// <param name="lpfnCB">A callback function to monitor progress or abort.</param>
    /// <returns>0 for okay.</returns>
    [DllImport("urlmon.dll", CharSet = CharSet.Auto, PreserveSig = false)]
    private static extern void URLDownloadToFile(
        [MarshalAs(UnmanagedType.IUnknown)] object pCaller,
        [MarshalAs(UnmanagedType.LPTStr)] string szURL,
        [MarshalAs(UnmanagedType.LPTStr)] string szFileName,
        Int32 dwReserved,
        IntPtr lpfnCB);

URLDownloadToFile(null, cidade_municipio + @"FormRelNFSe.aspx?tipo=emitirrelatorio&MostrarRel=false&idRec=verificarnfse&IdNotaEletronica=" + nfs + "&Expiration=23032015031453&Verificador=" + j, txtSalvar.Text +"\"+nfs.ToString() + "_" + j.ToString() + ".pdf", 0, IntPtr.Zero);
    
05.03.2015 / 15:55
2

Normally the easiest thing to do in this case is to download the file with the appropriate component:

using (var client = new System.Net.WebClient()) {
    client.DownloadFile("http://www.dominio.com.br/arquivo/1295889", "arquivo.pdf");
}

Method documentation .

    
24.11.2014 / 17:23
0

Just copy and paste the code below and see if it helps!

WebClient webclient = new WebClient();
webclient.DownloadFile(url_link, "C:meudocumentoem.pdf");
    
02.03.2017 / 00:31
-2

As per the Shadow Wizard response , from StackOverflow in English, assuming that the server sends the < > content-disposition :

using (WebClient client = new WebClient())
{
    using (Stream rawStream = client.OpenRead(url))
    {
        string fileName = string.Empty;
        string contentDisposition = client.ResponseHeaders["content-disposition"];
        if (!string.IsNullOrEmpty(contentDisposition))
        {
            string lookFor = "filename=";
            int index = contentDisposition.IndexOf(lookFor, StringComparison.CurrentCultureIgnoreCase);
            if (index >= 0)
                fileName = contentDisposition.Substring(index + lookFor.Length);
        }
        if (fileName.Length > 0)
        {
            using (StreamReader reader = new StreamReader(rawStream))
            {
                File.WriteAllText(Server.MapPath(fileName), reader.ReadToEnd());
                reader.Close();
            }
        }
        rawStream.Close();
    }
}

If you need to explain the answer here, comment out that I edit mine.

    
24.11.2014 / 17:52