【问题标题】:Download a file from a Variable URL c#从变量 URL c# 下载文件
【发布时间】:2014-06-05 15:54:01
【问题描述】:

我们被要求通过 url 从我们的客户网站下载订单文件。 我想做这样的事情。

string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;

// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
    myStringWebResource = remoteUri + fileName;
    // Download the Web resource and save it into the current filesystem folder.
    myWebClient.DownloadFile(myStringWebResource, fileName);        
}

但是 URL 将是可变的,因为我们必须在我们发布的 URL 中指定日期和时间。

而且我们下载的文件也是可变的。

由于我是 C# 的新手,我想就如何实现这一点提出一些建议?

提前致谢

【问题讨论】:

  • 你说 URL/File 是可变的,基于什么?你有一个可能的 URL 的例子吗?我看不出问题出在哪里?您不确定如何根据日期和时间构造 URL?

标签: c# variables url download


【解决方案1】:

这取决于 URL 的生成方式。他们遵循一个模式吗?你事先认识他们吗?

private void GetVariableFile(string remoteUri, string filename) {
string myStringWebResource = null;

// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient()) {
    myStringWebResource = remoteUri + fileName;
    // Download the Web resource and save it into the current filesystem folder.
    myWebClient.DownloadFile(myStringWebResource, fileName);
    //Do stuffs        
    }
}

您可能希望将 Uri 和获得的文件传递给处理下载和详细说明的方法,或者如果您需要返回结果,请更改返回类型,以便它可以返回您需要的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    相关资源
    最近更新 更多