现在的问题就是我拖出一个button和fileupload控件 要求上传到指定的地址中并以上传时间创建一个新的文件夹 这个怎么实现
解决方案 »
- m_CustomerList m_customerList which one is best normalise?
- 这个方法是什么意思??
- TheBeerHouse 中的数据库是sql server 2008哪个版本的
- 急急急 求手写分页 无控件的 请瞪大你的眼睛看清楚
- 为何REPEATER取不到TEXTBOX中的值。
- 哪位可以提供一下表情转换的UBB代码啊?就是[EM01]转换成<IMG...那种,谢谢啊!!
- maxlenth=20是指可以输入20个汉字吗?那可以输入多少个英文字母?标点符号怎么算?
- .Net分页控件发布
- 如何将数据库中的时间日期格式转换成短日期格式,并绑定到datalist中
- 哪里可以下载k3人力资源?
- 大家帮我实现一个功能啊。可能对你们来说是很简单的。。我菜啊。。
- 请大家看看www.wodeyichu.com为什么这个站文件都没扩展名呢?
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
string name = FileUpload1.FileName;
string type = FileUpload1.FileName.Substring(name.LastIndexOf(".") + 1).ToLower();
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("img"));
string upLoadTime = System.DateTime.Now.ToString("yyyyMMHHmmss");
dir.CreateSubdirectory(upLoadTime);
string ipath = Server.MapPath("img")+"\\"+upLoadTime + "\\"+name;
string wpath = "img" + "\\"+upLoadTime+ "\\" + name; if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")
{ FileUpload1.SaveAs(ipath);
Image1.Visible = true;
Image1.ImageUrl = wpath;
}
else
{
//
}
}
}
{ if (fulpicsmall.FileName == "")
{
ClientScript.RegisterStartupScript(typeof(string), "notnull", "<script>alert('请选择要上传的图')</script>");
fulpicsmall.Focus();
return;
} string expicName="jpg,img,gif,pci,bmp,pdf";//允许上传的图片的扩展名
string[] strarray=expicName.Split(',');//把expicname的字符串以逗号(,)分隔,放到数组中
string picstr=fulpicsmall.PostedFile.FileName;//要上传的文件全路径
string filename = picstr.Substring(picstr.LastIndexOf("\\") + 1);//原文件名,包括扩展名
string exfileName = picstr.Substring(picstr.LastIndexOf("."));//获取文件的扩展名,例如(.jpg)
string exfileNamepot=exfileName.Substring(1).ToLower();//获得exfileName字符串中从1开始到结束的字符串(获得扩展名除“.”的字符)
int picrange = fulpicsmall.PostedFile.ContentLength / 1024; //上传的文件大小(kb) if (picrange > 6300)
{
ClientScript.RegisterStartupScript(typeof(string), "morethan5m", "<script>alert('文件超过5M,不适合网络浏览')</script>");
} else
{//判断扩展名是否合法
bool IsNo = false;
foreach (string k in strarray)
{
if (exfileNamepot.ToString().Trim() == k.ToString().Trim())
{ IsNo = true;//如果是合法扩展名的图片,则IsNo为true;否则为false;
break;
}
}
if (!(bool)IsNo)
{//如果不是合法图片的扩展名
ClientScript.RegisterStartupScript(typeof(string), "notfit", "<script>alert('图片格式不合法')</script>");
} else
{//如果是合法图片格式 string fnewName = System.DateTime.Now.ToString("yyyyMMHHmmss");//新的时间为文件名(无扩展名)
string fallName = fnewName + exfileName;//新的文件名(包含扩展名)
string path = Server.MapPath("productspic\\");//文件夹new_pic的物理路径
path += fallName;//上传后的文件物理路径
fulpicsmall.PostedFile.SaveAs(path);//将要上传的文件保存到指定的路径下和文件名,path为物理路径
ViewState["productspicsmall"] = "productspic/" + fallName;//要保存到数据库里的文件路径
this.Image1.ImageUrl = (string)ViewState["productspicsmall"];//将页面图片显示为刚上传的图片
}
} }