Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

i want to download file automatically from browser,we need not show save dialog box.

VB
Private Sub DownloadFile(ByVal fullpath As String, ByVal forceDownload As Boolean)
        '    Dim path As Path
        Dim name = Path.GetFileName(fullpath)
        Dim ext = Path.GetExtension(fullpath)
        Dim type As String = ""

        If Not IsDBNull(ext) Then
            ext = LCase(ext)
        End If

        Select Case ext
            Case ".htm", ".html"
                type = "text/HTML"
            Case ".txt"
                type = "text/plain"
            Case ".doc", ".rtf"
                type = "Application/msword"
            Case ".csv", ".pdf"
                type = "Application/pdf"
            Case ".zip", ".ZIP"
                type = "Application/zip"
            Case ".zip", ".csv"
                type = "text/csv"
            Case Else
                type = "text/plain"
        End Select

        If (forceDownload) Then
            Response.AppendHeader("content-disposition", "attachment; filename=" + name)
        End If
        If type <> "" Then
            Response.ContentType = type
        End If

        Response.WriteFile(fullpath)

        Response.End()
    End Sub


What I have tried:

Tried the above code.
Posted
Updated 11-May-16 20:36pm
v2

1 solution

If you know the URL of the file you're trying to download, you can use the WebClient to do the login and the download. Just set the WebClient credentials and call its DownloadFile() method....
WebClient webClient = new WebClient();
webClient.Credentials = new System.Net.NetworkCredential("UserName", "Password", "Domain");
webClient.DownloadFile("fromURL", "C:\localfile.txt");


This is untested, by you may still be able to use your WebBrowser object to login, and then use a WebClient with the default credentials and call its DownloadFile() method:
WebClient webClient = new WebClient();
webClient.UseDefaultCredentials = true;
// or try webClient.Credentials = CredentialCache.DefaultCredentials;
webClient.DownloadFile("fromURL", "C:\localfile.txt");


OR you can try adding the OnFileDownload event. If it's called prior to the Save File dialog, then you can cancel the download, and launch the download through the WebClient object....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900