通过C#中的ActiveX下载文件

时间:2012-07-16 19:47:35

标签: c# .net html download activex

我有html启动ActiveX来下载文件。

我没有使用Internet Explorer下载文件,而是希望通过C#/ .NET

完成

HTML看起来像这样:

<HTML>
    <HEAD>
        <META http-equiv="Content-Type" content="text/html; charset=utf-8">
        <OBJECT ID="o" CLASSID="CLSID:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" CODEBASE="https://YYYYYY/grTransferCtrl.cab#version=5,0,0,32">
        </OBJECT>
        <SCRIPT LANGUAGE=VBScript FOR=window EVENT="onLoad()">

            On Error Resume Next

            o.Persist "AAA"
            o.Persist "BBB"
            o.Persist "CCC"

            bRetVal = o.Launch()
            If(Err.Number > 0 Or bRetVal = False) Then 
            msgbox "There was an error launching File Transfer Manager.",0,""
            End if

        </SCRIPT>
    </HEAD>
<BODY></BODY>


如何直接从我的C#应用​​程序启动下载?

2 个答案:

答案 0 :(得分:0)

您要查找的关键字是“WebRequest

以下将大致按照您的要求进行操作:

var request = WebRequest.Create("https://YYYYYY/grTransferCtrl.cab#version=5,0,0,32");

//This is where you may need to add cookies and other header data for this to work.
//I would use fiddler to try and inspect the http requests sent by the control for this
//information
request.UseDefaultCredentials = true;
var response = request.GetResponse();

using (var file= response.GetResponseStream())
{
    //do something with the stream? save it?
}   
response.Close();

作为替代方案,您可以在应用程序中创建ActiveX控件并使用它。 Web上有大量资源用于向.NET表单应用程序添加ActiveX控件,只是没有意识到这个特定控件对它可能阻止它的环境所做的假设。

答案 1 :(得分:0)

您可以使用System.Net.WebClient

来实现此目的
Example:

public class Downloader
{
  public void DownloadFile()
  {
     using(WebClient webClient = new WebClient())
     {
         webClient.DownloadFile("http://www.stackoverflow.com/stacks.txt", @"c:\stacks.txt");
     }
  }
}