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)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 郯城县| 藁城市| 乌鲁木齐县| 定日县| 九龙县| 休宁县| 广州市| 运城市| 南溪县| 洛川县| 九龙县| 临夏县| 台安县| 兴国县| 黄石市| 永川市| 华宁县| 绵阳市| 罗定市| 湖南省| 句容市| 临江市| 康保县| 宿迁市| 福安市| 内乡县| 西乌珠穆沁旗| 湖口县| 邹平县| 庆安县| 工布江达县| 什邡市| 伊金霍洛旗| 万荣县| 惠东县| 盐边县| 隆子县| 乌兰县| 枣强县| 吴桥县| 景东|