public bool Upload()
{
string username = HttpContext.Current.User.Identity.Name;
string file = fldPhoto.FileName; //
if (file.Length == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择要上传的文件');</script>");
return false;
}
else
{
if (fldPhoto.PostedFile.ContentLength >= 2000000) //2M
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('上传文件不能超过2M!');</script>");
return false;
}
else
{
return true;
}
}
}
public void UploadFiles()
{
string strCompanyName = "";
if (Request["uid"] != null)
{
strCompanyName = Request["uid"].Substring(0, 5) + "/" + Request["uid"].Substring(0, 9);
}
//获取文件名
string strPhotoName ="";
FileInfo file = new FileInfo(fldPhoto.PostedFile.FileName);
//Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + file.Extension + "');</script>");
//if (fldPhoto.PostedFile.ContentType == "image/gif" || fldPhoto.PostedFile.ContentType == "application/x-jpg" || fldPhoto.PostedFile.ContentType == "image/jpeg")
//{
if (file.Extension == ".jpg" || file.Extension == ".gif")
{
strPhotoName = StandardID() + file.Extension;
//获取相对地址
string strPhotoNamePath = Server.MapPath("File/TypeNotice/");
strPhotoName = System.IO.Path.GetFileName(strPhotoName);
//文件的安全地址
string strFullFileName = strPhotoNamePath + strCompanyName + "\\" + strPhotoName;
//文件存放文件夹地址
string strMapPath = strPhotoNamePath + strCompanyName;
//判断文件夹是否存在
try
{
if (System.IO.Directory.Exists(strMapPath))
{
if (System.IO.File.Exists(strFullFileName))
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('已经存在!');</script>");
}
else
{
//string inUploadFileName = StandardID();
//string inFileDir = this.Server.MapPath(strFullFileName) + file.Extension;
//string inFileDir = this.Server.MapPath("UploadFiles/") + inUploadFileName;
fldPhoto.SaveAs(strFullFileName);
//UploadFile.SaveAs(inFileDir);
txtFile.Text = strPhotoName;
//Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + fldPhoto.PostedFile.ContentLength.ToString() + "');</script>");
}
}
else
{
//创建文件夹
System.IO.Directory.CreateDirectory(strMapPath);
fldPhoto.SaveAs(strFullFileName);
txtFile.Text = strPhotoName;
}
}
catch (Exception e)
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('添加失败!');</script>");
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件类型错误!');</script>");
}
//}
//else
//{
// Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('文件类型错误!');</script>");
//}
}