当前位置:编程学习 > C#/ASP.NET >>

怎样实现图片上传?

图片上传 --------------------编程问答-------------------- HTML控件File Field 名字叫File1
web控件button 名字叫btnupload
web控件image  名字叫image1
在网页所在目录下新建一文件夹用作存放上传图片叫upload


====================================================

buttom改TEXT为上传
单击时间代码如下:
private void btnupload_Click(object sender, System.EventArgs e)
{
string fullfilename=this.File1.PostedFile.FileName;
string filename=fullfilename.Substring(fullfilename.LastIndexOf("\\")+1);
string type=fullfilename.Substring(fullfilename.LastIndexOf(".")+1);
if(type=="jpg"||type=="bmp"||type=="gif")
{
this.File1.PostedFile.SaveAs(Server.MapPath("upload")+"\\"+filename);
this.Image1.ImageUrl="upload/"+filename;

}
else
{
Response.Write("<script language='javascript'> alert('你选择的图片格式错误!');</script>");
}
} --------------------编程问答--------------------
 漏了一个~~忘记了说~`
   HTML那个控件File Field 要右键钩上"作为服务器控件运行" --------------------编程问答-------------------- 这里很详细:
http://tmsoft.lsxy.com/index.php?id=368&load=read --------------------编程问答--------------------    /// <summary>
    /// 上传图片
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        try
        {
            string test = Server.MapPath("ImageFiles/" + DateTime.Now.ToString("yyyy-MM-dd"));  //用来生成文件夹
            if (!Directory.Exists(test))
            {
                Directory.CreateDirectory(test);
            }
            if (FileUpload1.PostedFile.FileName != "")
            {
               string  imgname = FileUpload1.PostedFile.FileName;
               string imgType = imgname.Substring(imgname.LastIndexOf(".") + 1);
               string quanname =DateTime.Now.ToString("yyyyMMddHHmmss")+imgname.LastIndexOf("\\")+"."+imgType ;
               if ("gif" != imgType && "jpg" != imgType && "GIF" != imgType && "JPG" != imgType)
                {
                    Response.Write("<script>alert('请选择gif,jpg格式的文件!');</script>");
                    return;
                }
         
                string imgurl = "ImageFiles/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + quanname;
                 FileUpload1.PostedFile.SaveAs(Server.MapPath(imgurl));
               //////////////////////////////////////////////////////
                HttpCookie cookies = Request.Cookies["NewsUserCookies"];//cookies必须与登录是创建cookies一样
                string userid = cookies["News_userid"].ToString();
                string newitemno = getItemNoCong();//自动生成编号
                sqlt.Operate("insert into dbo.NewsAdd(newsid,userid,datetime)values('" + newitemno + "','" + userid + "',getdate())");
                sqlt.ConClose();
                string newsid = "select max(newsid) as newsid from dbo.NewsAdd"; 
                sqls.Fill_DT(newsid,dtNewsID);
                if (dtNewsID.Rows.Count>0)
                {
                //以下就执行更新 
                    sqlt.Operate("update dbo.NewsAdd set imgurl='" + imgurl + "' where  newsid ='" + dtNewsID.Rows[0]["newsid"].ToString() + "'");
                    sqlt.ConClose();
                    Response.Redirect("NewsAdd.aspx?newsid=" + dtNewsID.Rows[0]["newsid"].ToString() + "");
                    Response.Write(dtNewsID.Rows[0]["newsid"].ToString());
                }   
            }
            else
            {
                Response.Write("<script>alert('请选择文件上传!');</script>");
            }
        }

        catch (Exception ex)
        {
            throw ex;
        }
    }
}
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,