圖片上傳,網(wǎng)上有好多版本,今天也要做一個查了好多最終找到了一個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>
代碼前臺看起來很簡單。
實現(xiàn)的效果也還可以,不過這個保存的是base64的圖片,插入數(shù)據(jù)庫的時候我們一般都是保存xxxx.jpg 故需要實現(xiàn)一個方法
/// <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") + "http://"; 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; }
這里我是根據(jù)日期對文件夾分組了,并且保存到數(shù)據(jù)庫。避免單個文件太大
效果圖類似這樣
這個同樣也適用于手機上
總結
以上所述是小編給大家介紹的jquery.uploadView 實現(xiàn)圖片預覽上傳功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!
新聞熱點
疑難解答