/// <summary>
/// 下载服务器文件至客户端
/// </summary>
/// <param name="URL">被下载的文件地址,绝对路径</param>
/// <param name="Dir">另存放的目录</param>
public void Download(string URL, string Dir)
{
WebClient client = new WebClient();
string fileName = URL.Substring(URL.LastIndexOf("\\") + 1); //被下载的文件名
string Path = Dir;// Dir + "\\" + fileName; //另存为的绝对路径+文件名
try
{
client.DownloadFile(URL, fileName);
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] mbyte = r.ReadBytes((int)fs.Length);
FileStream fstr = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write);
fstr.Write(mbyte, 0, (int)fs.Length);
fstr.Close();
fs.Close(); 要关闭
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, "Error");
}
}
private void 下载导入模板ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog sd = new SaveFileDialog();
//sd.InitialDirectory = "C:\\";//默认目录
sd.Filter = "文本文件(*.xlsx;*xls)|*.xlsx;*xls";//可以保存的格式
sd.FileName = "大客户模板"; //默认文件名
if (sd.ShowDialog() == DialogResult.OK)
{
string url = Application.StartupPath + @"\..\..\Template\大客户模板.xlsx";
string path = sd.FileName;//
Download(url, path);
}
}
catch (Exception ex)
{ MessageBox.Show("下载失败,原因:"+ex.Message,"提示"); }
}
winform下载文件
最新推荐文章于 2023-04-25 16:42:12 发布