用fileupload控件上传文件怎样使上传的文件显示在指定的位置
2个回答
展开全部
protected string ImageBind()
{
bool flag = false;
string ss="";
string strPath = Server.MapPath("Image");//在aspx文件相对路径 Image文件
if (上传控件名称.FileName.Trim() == "")
{
Response.Write("<script>alert('请上传图片!');</script>");
return false;
}
#region 验证上传的图片格式是jpg,gif,png,bmp等几种格式
if (上传控件名称.HasFile)
{
string strFileExten = System.IO.Path.GetExtension(上传控件名称.FileName).ToLower().Trim();
string[] extFile = { ".jpg",".gif",".png",".bmp" };
for (int i = 0; i < extFile.Length; i++)
{
if (strFileExten == extFile[i])
flag = true;
}
}
#endregion
if (flag)//是图片格式
{
string time = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
+ DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + System.IO.Path.GetExtension(ful.FileName).ToLower().Trim();
上传控件名称.SaveAs(strPath + "\\" + time);//防止文件名一样
ss = strPath + "\\" + time;
flag=true;
}
else
{
Response.Write("<script>alert('上传图片格式不正确!!');</script>");
flag=false;
}
return ss;
}
然后直接去路径url=ImageBind();
{
bool flag = false;
string ss="";
string strPath = Server.MapPath("Image");//在aspx文件相对路径 Image文件
if (上传控件名称.FileName.Trim() == "")
{
Response.Write("<script>alert('请上传图片!');</script>");
return false;
}
#region 验证上传的图片格式是jpg,gif,png,bmp等几种格式
if (上传控件名称.HasFile)
{
string strFileExten = System.IO.Path.GetExtension(上传控件名称.FileName).ToLower().Trim();
string[] extFile = { ".jpg",".gif",".png",".bmp" };
for (int i = 0; i < extFile.Length; i++)
{
if (strFileExten == extFile[i])
flag = true;
}
}
#endregion
if (flag)//是图片格式
{
string time = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
+ DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + System.IO.Path.GetExtension(ful.FileName).ToLower().Trim();
上传控件名称.SaveAs(strPath + "\\" + time);//防止文件名一样
ss = strPath + "\\" + time;
flag=true;
}
else
{
Response.Write("<script>alert('上传图片格式不正确!!');</script>");
flag=false;
}
return ss;
}
然后直接去路径url=ImageBind();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询