我想使用用户名和密码从网站下载 excel 文件。登录后,有下载excel文件的链接。当我单击它时,另存为消息要求我保存在所需的文件夹中。我正在使用此功能,但它不起作用。它只是下载html页面。任何建议,将不胜感激。
ftpfullpath 是 url + 文件名
如果我使用登录表单 url,它会显示错误。
public static void DownloadFileFTP()
{
//Delete if Data feed file is already existed
if (File.Exists(GetFileName.source_path))
{
File.Delete(GetFileName.source_path);
}
//Create a WebClient
WebClient request = new WebClient();
//Set up credentials
request.Credentials = new NetworkCredential(Properties.Resources.UserName, Properties.Resources.Password);
//Download the data into Byte array
byte[] fileData = request.DownloadData(Properties.Resources.ftpfullpath);
//Create a Filestream to write to Byte array
FileStream sourceFile = File.Create(GetFileName.source_path);
try
{
//Write full byte array to the file
sourceFile.Write(fileData, 0, fileData.Length);
Console.WriteLine("Download Complete " + GetFileName.source_path);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (sourceFile != null)
{
sourceFile.Close();
sourceFile = null;
}
}
}