C#网络文件保存本地

string SaveFile(string address, ref string fileName)
{
    try
    {
        var path = sericeURL + @"\tempFile\" + DateTime.Now.ToString("yyyyMMdd") + @"\";
        var filePath = path + fileName;
        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);
        if (File.Exists(filePath))
        {
            fileName = "I" + fileName;
            filePath = path + fileName;
            return SaveFile(address, ref fileName);
        }
        using (WebClient client = new WebClient())
        {
            // address:从中下载数据的 URI,www.xxxx.com/xx.jpg
            // filePath:保存地址 URI,C:\\tempFile\\xx.jpg
            client.DownloadFile(address, filePath);
        }
        return filePath;
    }
    catch (Exception ex)
    {
        LogHelper.Log.Error("文件保存异常:", ex);
        throw ex;
    }
}

     

猜你喜欢

转载自blog.csdn.net/qq_32109957/article/details/90485741