/// <summary> /// 用来生成一个导出的文件名 /// </summary> /// <param name="dic"></param> /// <param name="path"></param> /// <returns></returns> public static string GetExportFileName(string dir, out string path) { path = "/Files/Export/" + dir; var guide = Guid.NewGuid().ToString(); path += "/" + guide + ".xlsx"; return(HttpContextHelper.GetPath(path)); }
/// <summary> /// 下载一个文件 /// </summary> /// <param name="url"></param> public static string DownFile(string url, string name) { WebClient client = new WebClient(); try { Stream str = client.OpenRead(url); StreamReader reader = new StreamReader(str); byte[] mbyte = new byte[1000000]; int allmybyte = (int)mbyte.Length; int startmbyte = 0; if (allmybyte <= 0) { return(""); } while (allmybyte > 0) { int m = str.Read(mbyte, startmbyte, allmybyte); if (m == 0) { break; } startmbyte += m; allmybyte -= m; } reader.Dispose(); str.Dispose(); string path = HttpContextHelper.GetPath(name); if (File.Exists(path)) { return(path); } FileStream fstr = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); fstr.Write(mbyte, 0, startmbyte); fstr.Flush(); fstr.Close(); return(path); } catch { return(""); } }