// 读取文件信息
string FileCate = System.Configuration.ConfigurationManager.AppSettings["fileCatelog"];
string FielPath = Server.MapPath("") +"\\"+ FileCate+"\\";
string[] fName = Directory.GetFiles(FielPath);
for (int i = 0; i < fName.Length; i++)
{
string fn="";
long len = 0;
string creatdate = "";
FileInfo fi1 = new FileInfo(fName[i].ToString());
if (fi1.Exists)
{
creatdate = fi1.CreationTime.ToString("yyyy年MM月dd日");
len = fi1.Length;
fileType = fi1.Extension.ToString();
}
fn = fName[i].Substring(fName[i].LastIndexOf("\\") + 1, fName[i].Length - fName[i].LastIndexOf("\\") - 1);
}
//保存上传的文件
private void UploadSaveAs()
{
int FileCount = Request.Files.Count;
if (FileCount > 0)
{
string[] AFileName;
string UploadPath;
UploadPath = System.Configuration.ConfigurationManager.AppSettings["fileCatelog"]+"\\";
char[] de ={ '\\' };
HttpPostedFile hpf = Request.Files[0];
AFileName = hpf.FileName.Split(de);
string UploadFileName = AFileName[AFileName.Length - 1];
if (UploadFileName != "" && hpf.ContentLength > 0)
{
if (hpf.ContentLength > 20000 * 1024)
{
Response.Write("<script language='javascript'>alert('文件太大,无法上传!');</script>");
return;
}
string FileCate = System.Configuration.ConfigurationManager.AppSettings["fileCatelog"];
string FielPath = Server.MapPath("") + "\\" + FileCate + "\\";
string[] fName = Directory.GetFiles(FielPath);
for (int i = 0; i < fName.Length; i++)
{
FileInfo fi1 = new FileInfo(fName[i].ToString());
if (fi1.Name == UploadFileName)
{
Response.Write("<script language='javascript'>alert('服务器上已经存在该文件,请选择其他文件或更改文件名重新上传');</script>");
//Response.Write("<script>alert('服务器上已经存在同名文件,请选择其他文件或更改文件名重新上传')</script>");
return;
}
}
string strFilepath = this.Request.FilePath;
string strPath = Path.GetDirectoryName(strFilepath);
string DestFileName = UploadPath + UploadFileName;
try
{
hpf.SaveAs(Server.MapPath(DestFileName));
this.lbMsg.Text = "文件上传成功!";
}
catch (Exception ex)
{
this.lbMsg.Text = ex.ToString();
}
}
else if (hpf.ContentLength >=0)
{
Response.Write("<script language='javascript'>alert('该文件内容为空,请选择其他文件!');</script>");
return;
}
}
}
//下载文件
1。超连接绑定到文件名
((HyperLink)this.GridView1.Rows[i].Cells[3].FindControl("HyperLink1")).NavigateUrl = FileCate + "/" + this.GridView1.Rows[i].Cells[0].Text;
2。直接写在table中
tc.Text = "<a title=\"点击下载\" href=\"" + 文件目录 + "\\" + 文件名+ "\">" + 文件名 + "</a>";