【原】FileUpload控件上传文件

代码
   
     
#region 上传文件
/// <summary>
/// 上传文件
/// </summary>
public void LoadFile()
{
if (FileUpload1.HasFile)
{
string fileContentType = FileUpload1.PostedFile.ContentType;
if (fileContentType == " image/bmp " || fileContentType == " image/gif " || fileContentType == " image/pjpeg " || fileContentType == " application/x-shockwave-flash " )
{
string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
FileInfo file = new FileInfo(name);
string fileName = file.Name; // 文件名称
string webFilePath = Server.MapPath( " ../Files/advert/ " + fileName); // 服务器端文件路径
string houzhui = System.IO.Path.GetExtension(name);
if ( ! File.Exists(webFilePath))
{
try
{
FileUpload1.SaveAs(webFilePath);
// 使用 SaveAs 方法保存文件
Response.Write( " <script>alert('文件上传成功!!')</script> " );
}
catch (Exception ex)
{
throw new Exception( " 提示:文件上传失败,失败原因 " + ex.Message);
}
}
else
{
Response.Write(
" <script>alert('文件已经存在,请重命名后上传!')</script> " );
}
}
else
{
Response.Write(
" <script>alert('文件类型不符!')</script> " );
}
}
else
{
Response.Write(
" <script>alert('请选要上传的文件!')</script> " );
}

}
#endregion

 

你可能感兴趣的:(fileupload)