你参考一下,我的程序:
private void b_add_Click(object sender, System.EventArgs e)
{
if (Finish_file.PostedFile!=null)
{
if (Finish_file.PostedFile.ContentLength>15728639)
{
//判斷上傳的附件大小,如果超過15兆就提示出錯!。
Response.Write("<script>alert('您不能上传大于15MB的附件,如果您看到这个对话框\\n1.请检查您的文件。\\n2.如果文件没错请分割成几个小于15MB的再上传。');</script>");
return;
}
string dir_path=Server.MapPath("/up_file/Finish_file");
dir_path+="\\"+DateTime.Today.Year;
if (!Directory.Exists(dir_path))
{
Directory.CreateDirectory(dir_path);
}
string nail_name=Path.GetExtension(Finish_file.PostedFile.FileName);
string file_name_only=Path.GetFileNameWithoutExtension(Finish_file.PostedFile.FileName);
if (file_name_only=="") return;
string file_name=dir_path+"\\"+file_name_only+nail_name;
int file_count=0;
while(File.Exists(file_name))
{
file_count+=1;
file_name=dir_path+"\\"+file_name_only+file_count+nail_name;
}
string posted_file_name=Path.GetFileName(Finish_file.PostedFile.FileName);
ListItem ltm=new ListItem(posted_file_name,file_name);
File_list.Items.Add(ltm);
File_list.SelectedIndex=File_list.Items.Count-1;
Finish_file.PostedFile.SaveAs(file_name); //保存文件。
}
else
{
}
}
private void b_add_Click(object sender, System.EventArgs e)
{
if (Finish_file.PostedFile!=null)
{
if (Finish_file.PostedFile.ContentLength>15728639)
{
//判斷上傳的附件大小,如果超過15兆就提示出錯!。
Response.Write("<script>alert('您不能上传大于15MB的附件,如果您看到这个对话框\\n1.请检查您的文件。\\n2.如果文件没错请分割成几个小于15MB的再上传。');</script>");
return;
}
string dir_path=Server.MapPath("/up_file/Finish_file");
dir_path+="\\"+DateTime.Today.Year;
if (!Directory.Exists(dir_path))
{
Directory.CreateDirectory(dir_path);
}
string nail_name=Path.GetExtension(Finish_file.PostedFile.FileName);
string file_name_only=Path.GetFileNameWithoutExtension(Finish_file.PostedFile.FileName);
if (file_name_only=="") return;
string file_name=dir_path+"\\"+file_name_only+nail_name;
int file_count=0;
while(File.Exists(file_name))
{
file_count+=1;
file_name=dir_path+"\\"+file_name_only+file_count+nail_name;
}
string posted_file_name=Path.GetFileName(Finish_file.PostedFile.FileName);
ListItem ltm=new ListItem(posted_file_name,file_name);
File_list.Items.Add(ltm);
File_list.SelectedIndex=File_list.Items.Count-1;
Finish_file.PostedFile.SaveAs(file_name); //保存文件。
}
else
{
}
}
int posi=myFilename.LastIndexOf("\\");
string fn=myFilename.Substring(posi);
myFilename.Substring(0,posi)是路径
(affi1和affi2是InputFile控件)
string path="";
string Fname="";
string Fname1="";
string Fname2="";
string webFname1="";
string webFname2="";
path=Request.ServerVariables["APPL_PHYSICAL_PATH"];
path=path.Replace("\\","\\\\"); if(affi1.PostedFile.ContentLength>0)
{
Fname1="affi"+System.DateTime.Now.Year+System.DateTime.Now.Month+ System.DateTime.Now.Day+
System.DateTime.Now.Hour+System.DateTime.Now.Minute+System.DateTime.Now.Second+System.DateTime.Now.Millisecond;
Fname2=affi1.PostedFile.FileName.Substring(affi1.PostedFile.FileName.Length-6,6);
Fname=path+"upload\\"+Fname1+"1"+Fname2;
webFname1="../upload/"+Fname1+"1"+Fname2;
affi1.PostedFile.SaveAs(Fname); } if(affi2.PostedFile.ContentLength>0)
{
Fname1="affi"+System.DateTime.Now.Year+System.DateTime.Now.Month+System.DateTime.Now.Day+
System.DateTime.Now.Hour+System.DateTime.Now.Minute+System.DateTime.Now.Second+System.DateTime.Now.Millisecond;
Fname2=affi2.PostedFile.FileName.Substring(affi2.PostedFile.FileName.Length-6,6);
Fname=path+"upload\\"+Fname1+"1"+Fname2;
webFname2="../upload/"+Fname1+"1"+Fname2;
affi2.PostedFile.SaveAs(Fname); }
string 文件名 = System.IO.Path.GetFileName();
string 路径 = System.IO.Path.GetFullPath();
string 文件名 = System.IO.Path.GetFileName(str_FileName);
string 路径 = System.IO.Path.GetFullPath(str_FileName);
ONCLICK事件里进行操作!
/////////////////////////////////////////////////////////////////
// 获取上传文件名
string strPhotoName = upload.PostedFile.FileName.ToString();
// 获取相对地址
string strPhotoNameMapPath = Server.MapPath ( "../../Photo/" ); strPhotoName = System.IO.Path.GetFileName( strPhotoName );// 文件的全地址
string strFullFileName = strPhotoNameMapPath+strCompanyName+"\\"+strPhotoName;
// 图片存入数据库的地址
string strPhotoPath = strCompanyName+"/"+strPhotoName;// 文件的存放文件夹地址
string strMapPath = strPhotoNameMapPath + strCompanyName; // 判断上传文件夹是否存在
if ( System.IO.Directory.Exists( strMapPath ) )
{
// 判断该图片已经上传
if( System.IO.File.Exists ( strFullFileName ) )
{
Response.Write( "该文件已经存在!" );
}
else
{
// 进行上传操作
upload.PostedFile.SaveAs( strFullFileName );
// 把文件地址放到数据库中
strSqlUserInfo="execute sp_tbPhoto_AddPhoto @strBlockID='"+strBlockID+"',@strCompanyID='"+strCompanyID+"',@strChild='"+strChild+"',@strCheckLink='"+strCheckLink+"',@strTitle='"+strTitle+"',@strPhotoType='"+strPhotoType+"',@strPhotoPath='"+strPhotoPath+"',@strLinkPath='"+strPhotoPath+"'";
clsCommon clsCom=new clsCommon(); // 采用WebService进行操作,你的可以用的方式来更新数据库!
Boolean dsRetu=clsCom.UpdateData(clsCommon.strCondbMemorabilia.ToString(),strSqlUserInfo);
Response.Redirect( "wfmPhotoAdd.aspx" );
}
}
else
{
// 创建文件夹
System.IO.Directory.CreateDirectory ( strMapPath );
upload.PostedFile.SaveAs( strFullFileName );
}