本文介绍了使用VB.NET自动下载文件而无需保存对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从浏览器自动下载文件,我们不需要显示保存"对话框.
i want to download file automatically from browser,we need not show save dialog box.
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.
推荐答案
WebClient webClient = new WebClient();
webClient.Credentials = new System.Net.NetworkCredential("UserName", "Password", "Domain");
webClient.DownloadFile("fromURL", "C:\localfile.txt");
这未经测试,您仍然可以使用WebBrowser对象登录,然后使用具有默认凭据的WebClient并调用其DownloadFile()方法:
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");
或者,您可以尝试添加OnFileDownload事件.如果在保存文件"对话框之前调用它,则可以取消下载,然后通过WebClient对象启动下载....
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....
这篇关于使用VB.NET自动下载文件而无需保存对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!