后台
public void Btn_click(){
HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile postFile = files["Filetype"];
string fileName = Path.GetFileName(postFile.FileName);
string path = SaveFiles(fileName, "Filetype");
}
private string SaveFiles(string FileName,string iFile)
{string imgPathPart = "";
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
try
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
//设置文件保存信息
//设置文件保存路径
string temPath = "diract" + @"\" + "" + FileName + "" + @"\" + DateTime.Now.Year + @"\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";
string pathHead = ConfigurationManager.AppSettings["doc_path"];
if (string.IsNullOrEmpty(pathHead))
{
pathHead = @"D:\Uploads\MIS\";
}
string path = pathHead + temPath;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
fileName = DateTime.Now.Ticks.ToString() + fileName;
postedFile.SaveAs(path + fileName);
imgPathPart = temPath + fileName;
}
else
{
imgPathPart = null;
}
}
catch (System.Exception Ex)
{
}
return imgPathPart;
}