JQ上传预览+存数据库
因为之前老师讲的方法有不少BUG 现在经过完善已经修复
之前老是讲的方法是每一张都会被传到后台文件夹里面去 导致在预览过程中如果刷新页面 那么预览的图片不能从后台文件夹中删除
这个方法实现在本地预览只要不点上传 就不会存后台文件夹 所以这个方法还是比较科学的
如果有什么问题欢迎大家留言指教!
下面是代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="jquery-1.11.2.min.js"></script>
</head> <style>
#uploadPreview {
width: 168px;
height: 168px;
background-position: center center;
background-size: cover;
border: 4px solid #fff;
-webkit-box-shadow: 0 0 1px 1px rgba(0, 0, 0, .3);
display: inline-block;
</style>
<body> <form action="chuli.php" method="post" enctype="multipart/form-data"target="shangchuan">
<input type="hidden" name="tp" value="" id="tp" /> <div id="uploadPreview"></div> <input id="uploadImage" type="file" name="file" class="fimg1" onchange="PreviewImage();" />
<input id="aa" type="submit" value="上传" />
</form>
<iframe style="display:none" name="shangchuan" id="shangchuan">
</iframe> </body>
</html> <script type="text/javascript">
function showimg(u)
{
var a1 = u;
$.ajax({
url:"add1.php",
data:{a1:a1},
type:"POST",
dataType:"text",
success:function(data){
if(data=="1")
{
alert("添加成功");
window.location.href="asd.php";
}
else
{
alert("");
} }
})
} $("aa").click(function(){
showimg(url);
}) $("#uploadImage").on("change", function(){ // Get a reference to the fileList
var files = !!this.files ? this.files : [];
// If no files were selected, or no FileReader support, return
if (!files.length || !window.FileReader) return; // Only proceed if the selected file is an image
if (/^image/.test( files[0].type)){ // Create a new instance of the FileReader
var reader = new FileReader(); // Read the local file as a DataURL
reader.readAsDataURL(files[0]); // When loaded, set image data as background of div
reader.onloadend = function(){ $("#uploadPreview").css("background-image", "url("+this.result+")"); } } }); </script>
上传处理页面代码:
<?php
if($_FILES["file"]["error"])
{
echo $_FILES["file"]["error"];
}
else
{
if(($_FILES["file"]["type"]=="image/jpeg" || $_FILES["file"]["type"]=="image/png")&& $_FILES["file"]["size"]<1024000)
{
$fname = "./imgg/".date("YmdHis").rand(100,1000).$_FILES["file"]["name"]; $filename = iconv("UTF-8","gb2312",$fname); if(file_exists($filename))
{
echo "<script>alert('该文件已存在!');</script>";
}
else
{ move_uploaded_file($_FILES["file"]["tmp_name"],$filename);
echo "<script>parent.showimg('{$fname}');</script>";
} }
else
{
echo "<script>alert('图片大小超过1M!');</script>";
}
} //<title>图片上传预览处理</title>
添加数据库代码:
算了不传了 太简单。。。。
JQ上传预览+存数据库的更多相关文章
- [前端 4] 使用Js实现图片上传预览
导读:今天做图片上传预览,刚开始的做法是,先将图片上传到Nginx,然后重新加载页面才能看到这个图片.在这个过程中,用户一直都看不到自己上传的文件是什么样子.Ps:我发现我真的有强迫症了,都告诉我说不 ...
- jQuery插件ImgAreaSelect 实例讲解一(头像上传预览和裁剪功能)
上一节随笔中,我们已经知道了关于jQuery插件ImgAreaSelect基本的知识:那么现在看一下实例: 首先,要知道我们应该实现什么功能? (1)图片能够实现上传预览功能 (2)拖拽裁剪图片,使其 ...
- 微信开发中使用微信JSSDK和使用URL.createObjectURL上传预览图片的不同处理对比
在做微信公众号或者企业微信开发业务应用的时候,我们常常会涉及到图片预览.上传等的处理,往往业务需求不止一张图片,因此相对来说,需要考虑的全面一些,用户还需要对图片进行预览和相应的处理,在开始的时候我使 ...
- 兼容好的JS图片上传预览代码
转 : http://www.codefans.net/articles/1395.shtml 兼容好的JS图片上传预览代码 (谷歌,IE11) <html xmlns="http:/ ...
- PHP WAMP 文件上传 及 简单的上传预览
...... 使用特殊的表单类型file, 主(上传)页面: <form action="chuli.php" method="post" enctype ...
- 单图上传预览(uploadpreview )
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ux.plup.File plupload 集成 ux.plup.FileLis 批量上传预览
//plupload 集成 Ext.define('ux.plup.File', { extend: 'Ext.form.field.Text', xtype: 'plupFile', alias: ...
- Jquery图片上传预览效果
uploadPreview.js jQuery.fn.extend({ uploadPreview: function (opts) { var _self = this, _this = $(thi ...
- Javascript之图片上传预览
使用Javascript之图片上传预览,我们无需上传到服务器中,兼容所有浏览器. 关键方法是使用微软库filter:progid:DXImageTransform.Microsoft.AlphaIma ...
随机推荐
- maven项目报Failed to read artifact descriptor
公司私服是个垃圾,处理自定义的jar包外,没有提供到中央仓库的路由,以至于通过maven下载jar包是老是报错. 折腾好久,最后在maven的update project时勾选了force updat ...
- 在父页面和其iframe之间函数回调 父页面回调iframe里写的函数
// @shaoyang 父页面 window['mengBanLogin']={ mengBanArr : new Array(), mengBanLoginSuccess : function( ...
- 20145329 《Java程序设计》课程总结
每周读书笔记链接汇总 •第一周读书笔记 http://www.cnblogs.com/jdy1453/p/5248592.html •第二周读书笔记 http://www.cnblogs.com/jd ...
- SaltStack部署配置Tomcat-第三篇
实验目标 简单部署tomcat及安装java环境 实现步骤 编写salt的状态模块 [root@linux-node1 web]# pwd /srv/salt/base/web [root@linux ...
- 建站有很多技术,如 HTML、HTML5、XHTML、CSS、SQL、JavaScript、PHP、http://ASP.NET、Web Services、浏览器脚本、服务器脚本等。它们的区别是什么?新手一点不懂,想理清所有这些技术之间的关系和应用范围。
先普及用户通过 浏览器 访问网页 的过程: 网页内容是通过服务器运算得出的结果,将结果(网页代码)传输给浏览器,网页代码再通过浏览器运算(计算.渲染),最终展示在用户的眼前的. 至此,我们知道了有2个 ...
- Vue2.0 + ElementUI的+ PageHelper实现的表格分页
参考博客:http://blog.csdn.net/u012907049/article/details/70237457 借鉴1.controller层编写 2.vue中,axios的写法(总页数等 ...
- 【Semantic Segmentation】Segmentation综述
部分转自:https://zhuanlan.zhihu.com/p/37618829 一.语义分割基本介绍 1.1 概念 语义分割(semantic segmentation) : 就是按照" ...
- sa learning
后缀数组之前一直在给队友搞,但是这个类太大了,预感到青岛八成会有,于是自己也学习一下,记录一下做题的历程 所用的模板暂时来自于队友的倍增nlogn da算法 int t1[maxn] , t2[max ...
- vue的seo方案 prerender-seo-plugin
利用vue cli 3.0安装脚手架.记住:勾选vue-router. 在vue.config.js里添加配置: 2, var path = require('path') 3, const Prer ...
- mvn deploy返回400错误的几种可能
user credentials are wrong url to server is wrong user does not have access to the deployment reposi ...