11-UploadFile

public class UploadFiles
{
/// <summary>
/// 公用上传文件方法
/// </summary>
/// <returns>上传后的文件完整路径</returns>
public static string UpLoadFile(HttpPostedFileBase getFile)
{
if (getFile != null)
{
string getPath = System.Web.HttpContext.Current.Server.MapPath("~\Image\");
if (!Directory.Exists(getPath))
{
Directory.CreateDirectory(getPath);
}
string newPath = Path.Combine(getPath, getFile.FileName);
getFile.SaveAs(newPath);
return "/Image/" + getFile.FileName;
}
else
{
return "";
}
}

public string ReadFile(string FName)
{
string FilePath = System.Web.HttpContext.Current.Server.MapPath("Files" + FName);
FileStream fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string Content = sr.ReadToEnd();
sr.Close();
fs.Close();
return Content;
}
}

原文地址:https://www.cnblogs.com/Wangyang11/p/10003736.html