圖片上傳並獲得圖片相對路徑保存在數據庫中


 

 //判斷上傳文件是否是圖片
        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();
            }
        }



 

前台放三個控件

前台代碼:

<asp: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"屬性。

 

 

 更多圖片上傳問題參考:http://leonardleonard.iteye.com/blog/276213

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM