活动手机网站开发,wordpress视频弹窗,网站导航条模板,logo设计大师使用《VB.net webbrowser 如何实现自定义下载 IDownloadManager》中的控件ExtendedWebBrowser#xff08;下载控件#xff09;#xff0c;并扩展了NewWindow2。
使用ExtendedWebBrowser_1过程中#xff0c;遇到很多问题#xff0c;花了几天时间#xff0c;终于解决了所有… 使用《VB.net webbrowser 如何实现自定义下载 IDownloadManager》中的控件ExtendedWebBrowser下载控件并扩展了NewWindow2。
使用ExtendedWebBrowser_1过程中遇到很多问题花了几天时间终于解决了所有问题。
问题1接管了下载后发现大文件下载主程序会阻塞。 一开始以为是在写文件时因为IO响应导致阻塞改用异步写等等...尝试发现阻塞依然看过《C# 用FileStream.WriteAsync 异步读文件调用线程还是被阻塞了》等文章后问题依然未能解决。 最后尝试主程序不写文件就是接管下载后在OnDataAvailable中对接收数据不操作不做任何操作阻塞依然存在。阻塞会导致下载中断而且这种情况与下载文件大小无关即使很小几M的文件也会发生。 苦思中记起以前写一个延时函数时为了不阻塞而加入了Application.DoEvents()语句于是在最频繁操作的OnDataAvailable中加入Application.DoEvents()语句问题终于得到完满解决。
问题2写文件 一开始是主程序写文件遇到太多麻烦在解决问题1中发现接管了下载后其实IE是有在后台进程中将文件下载到IE缓冲区的OnProgress第一次便是返回IE缓冲区中下载的文件名于是便改用等下载完后从IE缓冲区复制文件。貌似IE原有下载器也是这样干的
问题3对于会弹出新窗口的下载在进行第二次下载时没有触发下载。 因为主程序中接管弹出窗口的Extendedwebbrowser_2一直没关闭IE中下载窗口是立即关闭的并且发现在NewWindow2中将弹出下载转到Extendedwebbrowser_2触发下载时Extendedwebbrowser_2并没有触发DocumentCompleted估计就是这里导致第二次下载时不能触发。 解决办法在下载完后Extendedwebbrowser_2加载空白页about:blank问题解决。
下面是IWebBrowserDownloadManager接口完整代码
Imports Remotion.Dms.Clients.Windows.WebBrowserControl
Imports System.IONamespace MyDownloadmanager 定义接口以实现接口引用 2023.10.27Public Class MyDownloadmanagerImplements IWebBrowserDownloadManagerDim INetCacheFile As String一些状态的判定True、False其中的一种状态True或False必须放在接口内进行设定不能全部都靠通过外部进程来设定外部进程只进行其中一种状态的改变就好否则因为轮询时间差而导致不同步引发非预期结果发生。例如AfterLoadBlank#Region 增加属性将接口的数据传递出去以及传进来#Region 可读写属性Private _ContinueDownload As Boolean TruePublic Property ContinueDownload() As BooleanGetReturn _ContinueDownloadEnd GetSet(ByVal value As Boolean)_ContinueDownload valueEnd SetEnd PropertyPrivate _DownloadDir As String Public Property DownloadDir() As StringGetReturn _DownloadDirEnd GetSet(ByVal value As String)_DownloadDir valueEnd SetEnd PropertyPrivate _AttchmentFilename As String Public Property AttchmentFilename() As StringGetReturn _AttchmentFilenameEnd GetSet(ByVal value As String)_AttchmentFilename valueEnd SetEnd Property对于会弹出新窗口的下载因为程序中接管的webbrowser一直没关闭’在下载完后需要加载一次新页在这里加载空白页about:blank‘否则可能无法进行下一次下载。Private _AfterLoadBlank As Boolean FalsePublic Property AfterLoadBlank() As Boolean GetReturn _AfterLoadBlankEnd GetSet(ByVal value As Boolean)_AfterLoadBlank valueEnd SetEnd Property#End Region#Region ReadOnly PropertyDim _DownloadFileName As String Public ReadOnly Property DownloadFileName() As StringGetReturn _DownloadFileNameEnd GetEnd PropertyPrivate _totalSize As IntegerPublic ReadOnly Property GetTotalSize() As IntegerGetReturn _totalSizeEnd GetEnd PropertyPrivate _currentValue As IntegerPublic ReadOnly Property GetCurrentValue() As IntegerGetReturn _currentValueEnd GetEnd PropertyPrivate _success As BooleanPublic ReadOnly Property GetSuccess() As BooleanGetReturn _successEnd GetEnd PropertyPrivate _statusText As StringPublic ReadOnly Property GetStatusText() As StringGetReturn _statusTextEnd GetEnd PropertyPrivate _isAborted As BooleanPublic ReadOnly Property GetisAborted() As BooleanGetReturn _isAbortedEnd GetEnd PropertyPrivate _IsDownloadCompleted As Boolean False下载开始时设置为false下载结束或退出时设置为TruePublic ReadOnly Property IsDownloadCompleted() As Boolean GetReturn _IsDownloadCompletedEnd GetEnd PropertyPrivate _OnStartDownloading As Boolean False下载开始时设置为false下载结束或退出时设置为TruePublic ReadOnly Property OnStartDownloading() As Boolean GetReturn _OnStartDownloadingEnd GetEnd Property#End Region#End Region#Region 接口函数Public Sub OnAborted() Implements Remotion.Dms.Clients.Windows.WebBrowserControl.IWebBrowserDownloadManager.OnAbortedWriteRunLog(OnAborted)_isAborted True_IsDownloadCompleted True_OnStartDownloading FalseEnd SubPublic Function OnDataAvailable(ByVal buffer() As Byte, ByVal bytesAvailable As Integer) As Boolean Implements Remotion.Dms.Clients.Windows.WebBrowserControl.IWebBrowserDownloadManager.OnDataAvailable_currentValue bytesAvailable需要加这行否则可能下载时发生阻塞导致下载中断而且这种情况与下载文件大小无关即使很小几M的文件也会发生。Application.DoEvents() Return _ContinueDownloadEnd Function在下载完后需要加载一次新页在这里加载空白页about:blank否则可能无法进行下一次下载。Public Sub OnDownloadCompleted(ByVal success As Boolean, ByVal statusText As String) Implements Remotion.Dms.Clients.Windows.WebBrowserControl.IWebBrowserDownloadManager.OnDownloadCompleted_isAborted False_IsDownloadCompleted True_OnStartDownloading False_AfterLoadBlank True_success success_statusText statusText_AttchmentFilename WriteRunLog(OnDownloadCompleted success.ToString statusText)If success ThenIf String.IsNullOrEmpty(INetCacheFile) ThenWriteRunLog(没有找到IE缓冲区文件文件 _DownloadFileName 下载失败)ElseFile.Copy(INetCacheFile, _DownloadFileName, overwrite:True) 从IE缓冲区复制文件WriteRunLog(找到IE缓冲区文件 INetCacheFile 复制到 _DownloadFileName)End IfEnd IfEnd SubPublic Function OnProgress(ByVal currentValue As Integer, ByVal totalSize As Integer, ByVal statusText As String) As Boolean Implements Remotion.Dms.Clients.Windows.WebBrowserControl.IWebBrowserDownloadManager.OnProgress_totalSize totalSize从第一次OnProgress中获取下载文件名If String.IsNullOrEmpty(_DownloadFileName) Then _DownloadFileName MyGetFileName(statusText)If String.IsNullOrEmpty(_DownloadFileName) Then_DownloadFileName TmpFileEnd IfIf Directory.Exists(_DownloadDir) Then_DownloadFileName _DownloadDir \ _DownloadFileNameEnd IfEnd IfIf String.IsNullOrEmpty(INetCacheFile) ThenIf InStr(statusText, Windows\INetCache) 0 Then 查找IE缓冲区文件 保存路径、文件名INetCacheFile statusTextWriteRunLog(OnProgress找到IE缓冲区文件 INetCacheFile)End IfEnd IfSystem.Windows.Forms.Application.DoEvents()Return _ContinueDownloadEnd FunctionPublic Function OnStartDownload(ByVal uri As System.Uri) As Boolean Implements Remotion.Dms.Clients.Windows.WebBrowserControl.IWebBrowserDownloadManager.OnStartDownload_currentValue 0_OnStartDownloading True_ContinueDownload True_IsDownloadCompleted FalseINetCacheFile _DownloadFileName WriteRunLog(OnStartDownload uri.ToString)Return TrueEnd FunctionOnStartDownload:http://115.1.115.15:9080/FrntMonitor/servlet/com.icbc.cte.cs.servlet.CSReqServlet1、第一次获得文件名 OnProgress:0 totalSize:0 statusText: C:\Users\gdzs-liyh\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\0F2VEW6C\statatmdev[2].xls2、第二次获得下载链接 OnProgress:3483 totalSize:0 statusText:(OnStartDownload中的网址http://115.1.115.15:9080/FrntMonitor/servlet/com.icbc.cte.cs.servlet.CSReqServlet)3483就是在这时候读取的数据大小。OnProgress:3483 totalSize:0 statusText: C:\Users\gdzs-liyh\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\0F2VEW6C\statatmdev[2].xlsOnDataAvailable:38281 (这个就是文件的实际大小)可能文件小此时文件已经下载到缓存C:\Users\gdzs-liyh\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\0F2VEW6C\statatmdev[2].xlsOnProgress:38281 totalSize:0 statusText:(OnStartDownload中的网址)OnDownloadCompletedOnStartDownload http://115.96.14.11/kjbbs/UpLoadFile/2010-7/20107214401523292.doc1、第一次获得文件名 OnProgress:0 totalSize:0 statusText http://115.96.14.11/kjbbs/UpLoadFile/2010-7/20107214401523292.doc2、第二次获得下载链接 OnProgress:119296 totalSize:119296 statusText http://115.96.14.11/kjbbs/UpLoadFile/2010-7/20107214401523292.docOnProgress:119296 totalSize:119296 statusText C:\Users\gdzs-liyh\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\5Q44G40U\20107214401523292.docOnProgress:119296 totalSize:119296 statusText http://115.96.14.11/kjbbs/UpLoadFile/2010-7/20107214401523292.docOnDataAvailable:119296 可能文件小此时文件已经下载到缓存C:\Users\gdzs-liyh\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\0F2VEW6C\statatmdev[2].xlsOnDownloadCompleted#End RegionShared Sub WriteRunLog(ByVal MyMsg As String)Using w As StreamWriter File.AppendText(RunLog.txt)Dim w As StreamWriterIf File.Exists(RunLog.txt) ThenIf My.Computer.FileSystem.GetFileInfo(RunLog.txt).Length 10485760 Then 2017.5.4 文件大于10M清0w File.CreateText(RunLog.txt)w.Write(文件大于10M置0从头开始)w.Write(Chr(9))Elsew File.AppendText(RunLog.txt)End IfElsew File.CreateText(RunLog.txt)End Ifw.Write(Now)w.Write(Chr(9)) 插入Tab键w.WriteLine(MyMsg)w.Flush()w.Close()End UsingEnd SubPublic Function MyGetFileName(ByVal inStr As String) As String获取文件名 文件夹名、文件名不能包含下列字符\/:*?|Dim ss As Stringss System.IO.Path.GetFileName(inStr)Dim n InStrRev(ss, )If n ss.Length Thenss ss.Substring(n)End IfMyGetFileName ss.Replace(\, ).Replace(/, ).Replace(:, ).Replace(*, ).Replace(?, ).Replace(Chr(34), ).Replace(, ).Replace(, ).Replace(|, )If InStrRev(MyGetFileName, [) 1 And InStrRev(MyGetFileName, [) InStrRev(MyGetFileName, ]) Then 将 21018102002617385797[1] 中的 [1] 去掉MyGetFileName Left(MyGetFileName, InStrRev(MyGetFileName, [) - 1) Mid(MyGetFileName, InStrRev(MyGetFileName, ]) 1)End IfMyGetFileName _AttchmentFilename MyGetFileNameEnd FunctionEnd ClassEnd Namespace