ASP.NET上传控件上传图片

来源:百度知道 编辑:UC知道 时间:2024/05/22 10:43:02
使用上传控件上传图片时,如何将其名称重命名成永远不重复的名称,并结合扩展名形成一个新的完整的图片名称保存到数据库中?

using System.IO; //引入命名空间
//上传方法
private void Upimage()
{
if(File1.Value!="")
{
string fileContentType = File1.PostedFile.ContentType;//获取文件类型
//判断文件类型.只能是 BMP GIF pjpeg swf
if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg" || fileContentType == "image/swf")
{
//判断文件大小
if (File1.PostedFile.ContentLength / 1024 < 2000)
{
string name = File1.PostedFile.FileName; // 客户端文件路径
FileInfo file = new FileInfo(name);

//获取文件名称
//把当前时间取出,组成字符串,加入文件名称,防止重复命名
string fileName = System.DateTime.Now.ToString().Replace("-", "").Replace(" ", "").Replace(":", "") + file.Name;//文件名称
string pathss = Server.MapPath("Upimage") + "\\" + fileNa