以下是实现附件上传,下载和删除功能的一段代码:
上传:
string[] strLst = fu_up.PostedFile.FileName.Split('\\');
string filename = strLst[strLst.Length-1];//读取附件的名字
if (filename != "")
{
fu_up.PostedFile.SaveAs("" + Server.MapPath(".\\upload\\") + "" + filename);
}
下载:
string filename = "111.jpg";
string mPath = "" + Server.MapPath(".\\upload\\") + "" +filename ;//取文件路径
this.txt_load.Text = mPath.ToString();
System.IO.FileInfo file = new System.IO.FileInfo(mPath);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(file.FullName);
Response.End();
}
删除:
string filename = "111.jpg";
string path = "" + Server.MapPath(".\\upload\\") + "" + filename.ToString();
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
file.Delete();
}