图片或者文件上传控件的使用

1:拖入FileUpload控件

  
    
商品图片 < asp:FileUpload ID ="FileUpload1" runat ="server" />
< asp:Button ID ="Button1" runat ="server" Text ="上传" onclick ="Button1_Click" />

2:拖入预览图片控件Image

  
    
< asp:Image ID ="Image1" runat ="server" ImageUrl ="../upload/none.jpg"
ToolTip
="none.jpg" Height ="140px" Width ="220px" />
// ToolTip="none.jpg" 注 意很重要

3:在三层外的公共类库utility中建立文件tool.cs  注意要添加引入System.web

  
    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net.Mail;
using System.Net;
namespace xh.shop.Utility
{
public class tool
{
/// <summary> 过滤非法字符串
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string GetSafeSQL( string value)
{
if ( string .IsNullOrEmpty(value))
return string .Empty;
value
= Regex.Replace(value, @" ; " , string .Empty);
value
= Regex.Replace(value, @" ' " , string .Empty);
value
= Regex.Replace(value, @" & " , string .Empty);
value
= Regex.Replace(value, @" %20 " , string .Empty);
value
= Regex.Replace(value, @" -- " , string .Empty);
value
= Regex.Replace(value, @" == " , string .Empty);
value
= Regex.Replace(value, @" < " , string .Empty);
value
= Regex.Replace(value, @" > " , string .Empty);
value
= Regex.Replace(value, @" % " , string .Empty);
return value;
}
/// <summary> 上传文件方法
/// 返回文件名
/// </summary>
/// <param name="myFileUpload"> 上传控件ID </param>
/// <param name="allowExtensions"> 允许上传的扩展文件名类型,如:string[] allowExtensions = { ".doc", ".xls", ".ppt", ".jpg", ".gif" }; </param>
/// <param name="maxLength"> 允许上传的最大大小,以M为单位 </param>
/// <param name="savePath"> 保存文件的目录,注意是绝对路径,如:Server.MapPath("~/upload/"); </param>
public static string Upload(FileUpload myFileUpload, string [] allowExtensions, int maxLength, string savePath)
{
// 文件格式是否允许上传
bool fileAllow = false ;

// 检查是否有文件案
if (myFileUpload.HasFile)
{
// 检查文件大小, ContentLength获取的是字节,转成M的时候要除以2次1024
if (myFileUpload.PostedFile.ContentLength / 1024 / 1024 >= maxLength)
{
throw new Exception( " 只能上传小于 " + maxLength + " M的文件! " );
}

// 取得上传文件之扩展文件名,并转换成小写字母
string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
string tmp = "" ; // 存储允许上传的文件后缀名
// 检查扩展文件名是否符合限定类型
for ( int i = 0 ; i < allowExtensions.Length; i ++ )
{
tmp
+= i == allowExtensions.Length - 1 ? allowExtensions[i] : allowExtensions[i] + " , " ;
if (fileExtension == allowExtensions[i])
{
fileAllow
= true ;
}
}

if (fileAllow)
{
try
{
string datedir = DateTime.Now.ToString( " yyyyMMdd " );
if ( ! Directory.Exists(savePath + datedir))
{
Directory.CreateDirectory(savePath
+ datedir);
}
string saveName = Guid.NewGuid() + fileExtension;
string path = savePath + datedir + " / " + saveName;
// 存储文件到文件夹
myFileUpload.SaveAs(path);
return datedir + " / " + saveName;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
else
{
throw new Exception( " 文件格式不符,可以上传的文件格式为: " + tmp);
}
}
else
{
throw new Exception( " 请选择要上传的文件! " );
}
}

}
}

4:前台拖入Btn按钮 点击进入后台

  
    
protected void Button1_Click( object sender, EventArgs e)
{
try
{
string proimg = xh.shop.Utility.tool.Upload(FileUpload1, new string [] { " .jpg " , " .gif " , " .png " }, 2 , Server.MapPath( " ~/upload/ " ));
// 文件类型与大小保存路径

Image1.ImageUrl
= " ~/upload/ " + proimg;
// 预览路径
Image1.ToolTip = proimg;
}
catch (Exception ex)
{
Response.Write(
" <script language='JavaScript'>alert(' " + ex.Message + " ')</script> " );


}
}

你可能感兴趣的:(文件上传)