Home > Web Front-end > JS Tutorial > body text

Example of how jQuery uses uploadView to implement image preview upload function

黄舟
Release: 2017-08-11 13:56:35
Original
2292 people have browsed it

图片上传,网上有好多版本,今天小编给大家分享jquery.uploadView 实现图片预览上传功能,感兴趣的的朋友一起看看吧

图片上传,网上有好多版本,今天也要做一个查了好多最终找到了一个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>
 <p>
  <p class="shangchuan">
   <h4>示例</h4>
   <p class="js_uploadBox" style="position: relative">
    <p 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" />
    </p>
    <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()">
   </p>
   <input type="hidden" id="hidTmp_ID" name="Tmp_ID" value="" />
  </p>
 </p>
</body>
</html>
<script type="text/javascript">
 function Upload() {
  $("#id").uploadView({
   uploadBox: &#39;.js_uploadBox&#39;,//设置上传框容器
   showBox: &#39;.js_showBox&#39;,//设置显示预览图片的容器
   width: &#39;100&#39;, //预览图片的宽度,单位px
   height: &#39;100&#39;, //预览图片的高度,单位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>
Copy after login

代码前台看起来很简单。

实现的效果也还可以,不过这个保存的是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;
  }
Copy after login

这里我是根据日期对文件夹分组了,并且保存到数据库。避免单个文件太大

效果图类似这样

 

这个同样也适用于手机上

总结

The above is the detailed content of Example of how jQuery uses uploadView to implement image preview upload function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!