当前位置: 移动技术网 > IT编程>开发语言>.net > 图片上传并获得图片相对路径保存在数据库中

图片上传并获得图片相对路径保存在数据库中

2018年11月04日  | 移动技术网IT编程  | 我要评论

网络特级追杀令,bilbilibi,qq群搞笑图片

//判断上传文件是否是图片
        private static bool isallowedextension(fileupload upfile)
        {
            string stroldfilepath = "";
            string strextension = "";
            string[] arrextension = { ".gif", ".jpg", ".bmp", ".png" };
            if (upfile.postedfile.filename != string.empty)
            {
                stroldfilepath = upfile.postedfile.filename;//获得文件的完整路径名 
                strextension = stroldfilepath.substring(stroldfilepath.lastindexof("."));//获得文件的扩展名,如:.jpg 
                for (int i = 0; i < arrextension.length; i++)
                {
                    if (strextension.equals(arrextension[i]))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        //图片上传并将图片重命名
        protected void fileupload_button_click(object sender, eventargs e)
        {
            try
            {
                if (fileupload1.hasfile)
                {
                    if (isallowedextension(fileupload1))
                    {
                        string filepath = fileupload1.filename;
                        //string filename = filepath.substring(filepath.lastindexof('\\') + 1, filepath.length - filepath.lastindexof('\\') - 1);//(filepath.lastindexof("\\") + 1);
                        //以年-月-日-时-分-秒-毫米将图片重新命名
                          string filename = datetime.now.tostring("yyyy-mm-dd-hh-mm-ss-fffffff")+filepath.substring(filepath.lastindexof('.'), filepath.length - filepath.lastindexof('.'));
                        //设定上传路径(绝对路径)
                          string uppath = server.mappath("~/photo/") + filename;
                        //图片上传至绝对路径
                          fileupload1.postedfile.saveas(uppath);
                        //设定的存储路径
                          string savepath="~\\photo\\"+filename;
                        hiddenfield1.value = savepath;
                        lblinfo.text = "上传成功!";
 
                    }
                }
                else
                {
                    lblinfo.text = "请选择上传文件";
                }
            }
            catch (exception ex)
            {
                lblinfo.text = "上传发生错误!原因是:" + ex.tostring();
            }
        }
 
<br>
<br>
 
前台放三个控件
前台代码:
?
<:fileupload id="fileupload1" runat="server" font-size="13px" bordercolor="gray" borderstyle="solid" borderwidth="1px" height="25px" width="400px"  />
<asp:button id="fileupload_button" runat="server" text="上传图片" height="25px"
      causesvalidation="false" onclick="fileupload_button_click"  /> 
<asp:label id="lblinfo" runat="server" forecolor="red" font-size="13px" ></asp:label>
 
 
注:
1、fileupload 控件不能放在updatepanel控件内,如果放在updatepanel内fileupload1.hasfile始终为false
2、在给insert语句填充数据时,要使用cmd.parameters.addwithvalue("@b", savepath);的格式防止字符串savepath中的'\'转义,若使用string sql=string.format("insert into table1(path) values('{0}')",savepath);的格式,存入数据库的图片路径将会没有'\'。
3、若在图片上传的页面中有验证控件,且不想在单击图片上传按钮的过程中进行验证,需要加上causesvalidation="false"属性。

 


摘自 miko2012


 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网