qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          如何將上傳圖片直接保存至SQL Server數據庫中

            這里演示的是如何將上傳圖片直接保存至SQL Server數據庫中。

            在數據庫中,圖片使用varbinary(MAX)存儲:

            這是個基于MVC3架構的例子。廢話不多說,直接上代碼:

            View:

          @{
              ViewBag.Title = "UpLoadImg";
          }
          @using (Html.BeginForm("Create", "UpLoadImg", FormMethod.Post, new { enctype = "multipart/form-data" }))
          {
              <h2>
                  UpLoadImg</h2>
              <div id="mainform">
                  <div>
                      <input type="file" id="UpLoadFile" name="UpLoadFile" />
                      <input id="btnUpLoad" type="submit" value="上傳" />
                  </div>
                  <div>
                  </div>
              </div>
          }

            Controller后臺Action:

          public ActionResult Create()
                  {
                      string filename = string.Empty;
                      string filetype=string.Empty;
                      byte[]  filecontext=null;
                      HttpPostedFileBase filebase = Request.Files["UpLoadFile"];
                      if (filebase.ContentLength > 0)
                      {
                          Stream stream = filebase.InputStream;
                          byte[] by = new byte[filebase.ContentLength];
                          int i = stream.Read(by,0,filebase.ContentLength);
                          stream.Close();
                          string[] arrs = filebase.FileName.Split('\\');
                          if (arrs.Length > 0)
                          {
                              filename = arrs[arrs.Length - 1];
                          }
                          else
                          {
                              filename = filebase.FileName;
                          }
                          filetype=filebase.ContentType;
                          filecontext=by;
                      }//淡雅一抹繁華,幾多思念許他,他不知花開不易,他不懂人心需要珍惜。
                      int count = 0;
                      #region 插入數據
                      try
                      {
                          string ImageStore = System.Configuration.ConfigurationManager.AppSettings["ConnectionStrImageStore"].ToString().Trim();
                          string sqlStr = string.Empty;
                          sqlStr = @"INSERT INTO [Images] ([filename],[filetype],[filecontext],[uploadtime])
                              VALUES(@filename,@filetype,@filecontext,@uploadtime)";
                          SqlConnection connection = new SqlConnection(ImageStore);
                          SqlCommand command = new SqlCommand(sqlStr, connection);
                          command.Parameters.AddWithValue("@filename",filename);
                          command.Parameters.AddWithValue("@filetype",filetype);
                          command.Parameters.AddWithValue("@filecontext",filecontext);
                          command.Parameters.AddWithValue("@uploadtime",DateTime.Now);
                          command.Connection.Open();
                          count=command.ExecuteNonQuery();
                          command.Connection.Close();
                      }
                      catch
                      {
                         
                      }
                      #endregion

                      if (count > 0)
                      {
                          return RedirectToAction("UpLoadImg");
                      }
                      else
                      {
                          return RedirectToAction("Index");
                      }
                  }

          posted on 2013-09-13 10:55 順其自然EVO 閱讀(896) 評論(0)  編輯  收藏 所屬分類: 數據庫

          <2013年9月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 大同市| 松原市| 肇东市| 四平市| 尼木县| 息烽县| 桐乡市| 绥棱县| 延吉市| 东平县| 衡阳市| 桂东县| 正阳县| 长垣县| 马山县| 利津县| 信丰县| 临漳县| 三穗县| 龙川县| 宜章县| 邢台县| 澄江县| 新安县| 正蓝旗| 江安县| 玉田县| 旌德县| 都匀市| 泽州县| 靖安县| 贵港市| 清水县| 黄石市| 六枝特区| 和平区| 桦甸市| 新干县| 济南市| 邵武市| 绍兴市|