<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qileilove

    blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

    如何將上傳圖片直接保存至SQL Server數(shù)據(jù)庫中

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

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

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

      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 插入數(shù)據(jù)
                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 閱讀(893) 評論(0)  編輯  收藏 所屬分類: 數(shù)據(jù)庫

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

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费无码又爽又高潮视频| 亚洲VA中文字幕无码一二三区| 国产亚洲女在线线精品| 中文字幕精品亚洲无线码一区| 人妻无码久久一区二区三区免费| 国产成人亚洲精品| 成人午夜亚洲精品无码网站| 日本免费xxxx色视频| 免费人成视频在线播放| 五月天网站亚洲小说| 国产精品免费视频播放器| a毛片在线看片免费| 亚洲精品第一国产综合亚AV| 久久精品国产精品亚洲精品 | 老妇激情毛片免费| 亚洲人成在线影院| 国产91久久久久久久免费| 男女作爱在线播放免费网站| 亚洲精品女同中文字幕| 亚洲精品免费观看| www国产亚洲精品久久久日本| 50岁老女人的毛片免费观看| 成在线人直播免费视频| 亚洲高清中文字幕免费| 久久久久久久久亚洲| 亚洲AⅤ无码一区二区三区在线| 131美女爱做免费毛片| a级毛片免费全部播放无码| 亚洲а∨精品天堂在线| 亚洲欧洲精品视频在线观看| 国产亚洲精品成人a v小说| 午夜a级成人免费毛片| 亚洲电影在线免费观看| 三年片在线观看免费| 黄色网址在线免费观看| 亚洲情A成黄在线观看动漫软件| 亚洲欧洲日韩不卡| 亚洲精品无码午夜福利中文字幕 | 免费无码又爽又黄又刺激网站| 亚洲AV无码乱码麻豆精品国产| 久久夜色精品国产亚洲AV动态图 |