日期:2014-05-18  浏览次数:20897 次

C# FileUpload控件上传图片和文件
[size=large][/size][color=yellow][/color]//获取文件名称
string fileName=FileUpload1.FileName;
//获取文件的大小
string fileSize=FileUpload1.PostedFile.ContentLength.ToString();
//获取文件的类型
string flieType=FileUpload1.PostedFile.ContentType;
//获取文件的后缀名
string fileNameType=fileName.Substring(fileName.LastIndexOf("."+1);
//是图片的保存路径;img为服务器目录上的文件夹
string ImgSavePath=Server.MapPath(Request.ApplicationPath+"/img")+"\\"+fileName;
//是文件的保存路径;file为服务器上目录上的文件夹
string FileSavePath=Server.MapPath(Request.applicationPath+"/file")+"\\"+fileName;
//在项目上的路径
string projectPath=Request.ApplicationPath+"img/"+fileName;
if(fileNameType=="jpg"||fileNameType=="bmp"||fileNameType=="gif"){
     FileUpload1.SaveAs(ImgSavePath);
     Image1.ImageUri=projectPth;
}
else
{
   FileUpload1.SaveAs(FileSavePath);
   Image.Visible=false;
}