jquery.uploadView 實現圖片預覽上傳

來源:互聯網
上載者:User

標籤:xxx   out   技術   height   cto   tap   hmm   func   hang   

圖片上傳,網上有好多版本,今天也要做一個查了好多最終找到了一個uploadview 進行了一下修改

來看代碼

@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Index</title>    <script src="~/Scripts/jquery-1.8.2.min.js"></script>    <script src="~/Scripts/jquery.uploadView.js"></script></head><body>    <div>        <div class="shangchuan">            <h4>樣本</h4>            <div class="js_uploadBox" style="position: relative">                <div id="preview" class="js_showBox">                    <img id="imghead" border="0" src="http://static.neihanhongbao.com/highads/images/nologo.jpg" alt="上傳圖片" style="width:100px; height:100px" />                </div>                <input type="file" name="file" id="id" style="position: absolute; top: 0px; left:0px; height: 100px; filter: alpha(opacity:0); opacity: 0; width: 100px" onclick="Upload()">            </div>            <input type="hidden" id="hidTmp_ID" name="Tmp_ID" value="" />        </div>    </div></body></html><script type="text/javascript">    function Upload() {        $("#id").uploadView({            uploadBox: ‘.js_uploadBox‘,//設定上傳框容器            showBox: ‘.js_showBox‘,//設定顯示預覽圖片的容器            width: ‘100‘, //預覽圖片的寬度,單位px            height: ‘100‘, //預覽圖片的高度,單位px            allowType: ["gif", "jpeg", "jpg", "bmp", "png"], //允許上傳圖片的類型            maxSize:1, //允許上傳圖片的最大尺寸,單位M            success: function (e) {                var l = $(".js_showBox img").attr("src");                $("#hidTmp_ID").val(l);            }        });    }</script>

代碼前台看起來很簡單。

實現的效果也還可以,不過這個儲存的是base64的圖片,插入資料庫的時候我們一般都是儲存xxxx.jpg 故需要實現一個方法

 /// <summary>        /// base64轉圖片        /// </summary>        /// <returns></returns>        public static string BaseToImg(string baseimg) {            byte[] bt = Convert.FromBase64String(baseimg.Replace("data:image/jpeg;base64,", ""));            string filepath = "ImgServer".GetAppsetting();            string sqlurl = @"" + DateTime.Now.ToString("yyyyMMdd") + "\\";            string sqlname = "" + DateTime.Now.ToString("yyyyMMddhhmmssmsfff") + ".jpg";            if (!System.IO.File.Exists(filepath + sqlurl)) {                System.IO.Directory.CreateDirectory(filepath + sqlurl);                System.IO.File.WriteAllBytes(filepath + sqlurl + sqlname, bt);            }            return sqlurl + sqlname;        }

這裡我是根據日期對檔案夾分組了,並且儲存到資料庫。避免單個檔案太大

類似這樣

 

 這個同樣也適用於手機上

 

忘了附上下載連結:http://pan.baidu.com/s/1boMcO6j

 

jquery.uploadView 實現圖片預覽上傳

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.