form里面文件上传并预览
其实form里面是不能嵌套form的,如果form里面有图片上传和其他input框,我们希望上传图片并预览图片,然后将其他input框填写完毕,再提交整个表单的话,有两种方式!
方式一:点击上传按钮的链接弹出上传页面,上传文件,上传完毕再返回表单页面;这种比较简单,其实就是表单页面的上传按钮仅仅是一个链接,仅用于打开上传文件的弹出页面;
方式二:就是表单里面有<input type="file" name="picture"/>,点击上传按钮后,会在上传按钮旁边有图片预览,这种其实图片也没有上传到服务器,而是将图片做了个本地预览,当填写完其他input框内容,提交后才开始上传的!
其完整代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
<script type= "text/javascript" src= "jquery-3.2.1.js" ></script> <script type= "text/javascript" > function showImg(obj){ var objUrl = getObjectURL(obj.files[0]); if (objUrl) { $(obj).before( '<img src="' + objUrl + '" alt="" width="100px;"> ' ); } } function getObjectURL(file) { var url = null ; if (window.createObjectURL!=undefined) { url = window.createObjectURL(file) ; } else if (window.URL!=undefined) { url = window.URL.createObjectURL(file) ; } else if (window.webkitURL!=undefined) { url = window.webkitURL.createObjectURL(file) ; } return url ; } </script> //文件上传表单 <form method= "post" action= "index.php" enctype= "multipart/form-data" > <input name= "picture" id= "picture" style= "position:absolute;clip:rect(0 0 0 0);" onchange= "showImg(this);" type= "file" > <label style= "cursor:pointer;width:80px;height:30px;text-align:center;line-height:30px;color:#FFFFFF;box-shadow: 2px 2px 2px #888888;" for = "picture" >上传LOGO</label> <input name= "sub" type= "submit" value= "提交" /> </form> //文件上传php代码 <?php $file = @ $_FILES [ 'picture' ]; //得到传输的数据 //得到文件名称 $name = $file [ 'name' ]; $type = strtolower ( substr ( $name , strrpos ( $name , '.' )+1)); //得到文件类型,并且都转化成小写 $allow_type = array ( 'jpg' , 'jpeg' , 'gif' , 'png' ); //定义允许上传的类型 //判断文件类型是否被允许上传 if (!in_array( $type , $allow_type )){ //如果不被允许,则直接停止程序运行 return ; } //判断是否是通过HTTP POST上传的 if (! is_uploaded_file ( $file [ 'tmp_name' ])){ //如果不是通过HTTP POST上传的 return ; } $upload_path = "D:/now/" ; //上传文件的存放路径 //开始移动文件到相应的文件夹 if (move_uploaded_file( $file [ 'tmp_name' ], $upload_path . $file [ 'name' ])){ return $upload_path . $file [ 'name' ]; } else { echo "Failed!" ; } ?> |
form里面文件上传并预览的更多相关文章
- form表单系列中文件上传及预览
文件上传及预览 Form提交 Ajax 上传文件 时机: 如果发送的[文件]:->iframe, jQurey(),伪Ajax 预览 import os img_path = os.path.j ...
- JavaScrip 原生多文件上传及预览 兼容多浏览器
JavaScrip 原生多文件上传及预览 兼容多浏览器 html代码块 <div class="container"> <label>请选择一个图像文件:& ...
- 结合bootstrap fileinput插件和Bootstrap-table表格插件,实现文件上传、预览、提交的导入Excel数据操作流程
1.bootstrap-fileinpu的简单介绍 在前面的随笔,我介绍了Bootstrap-table表格插件的具体项目应用过程,本篇随笔介绍另外一个Bootstrap FieInput插件的使用, ...
- servlet实现文件上传,预览,下载和删除
一.准备工作 1.1 文件上传插件:uploadify: 1.2 文件上传所需jar包:commons-fileupload-1.3.1.jar和commons-io-2.2.jar 1.3 将数 ...
- Django的文件上传以及预览、存储
思路: 文件上传通过前端的input标签,input设置display:none属性. 内容显示需要让前端通过<img>标签读取图片内容,可以通过<label>标签连接< ...
- 支持多文件上传,预览,拖拽,基于bootstra的上传插件fileinput 的ajax异步上传
首先需要导入一些js和css文件 <link href="__PUBLIC__/CSS/bootstrap.css" rel="stylesheet"&g ...
- 支持多文件上传,预览,拖拽,基于bootstrap的上传插件fileinput 的ajax异步上传(转载)
首先需要导入一些js和css文件 <link href="__PUBLIC__/CSS/bootstrap.css" rel="stylesheet"&g ...
- web端文件上传,预览,下载,删除
//HTML部分 <div class="item attachment attachmentNew"> <span class="name&quo ...
- JQ图片文件上传之前预览功能
1.先准备一个div onchange触发事件 <input type="file" onchange="preview(this)" >< ...
随机推荐
- iOS 版本更新迭代
开发中我们可能会遇到这样的需求,当AppStore中有新版本迭代更新,在用户点开APP的时候弹框提醒客户去AppStore更新APP.这里面就有个关键点,判断当前APP与AppStore中的版本高低, ...
- linux新装系统优化
1:关掉不需要的服务 检查在3级别上哪些是自动启动的 chkconfig --list |grep ‘3:on’
- ValidationUtil
package me.zhengjie.common.utils; import me.zhengjie.common.exception.BadRequestException; import ja ...
- python函数参数理解
1.位置参数 函数调用时,参数赋值按照位置顺序依次赋值. e.g. def function(x): 3 return x * x 5 print function(2) 输出结果: 4 def fu ...
- Invalid action class configuration that references an unknown class解决方案
Sturts2整合后时出现诡异的异常: java.lang.RuntimeException: Invalid action class configuration that references a ...
- WPF中,如何屏蔽WebBrowser弹出的脚本错误窗口?
WPF没有自带屏蔽这些窗口的方法或属性,可以使用反射的方法来屏蔽弹出脚本错误窗口: public void SuppressScriptErrors(WebBrowser wb, bool Hide) ...
- .net和JAVA面向对象,继承有趣的细节
原型是同事间讨论的一道面试题.估计这题秒杀了不少人,LZ也被秒了. 但这个题里隐藏了一个很有趣的细节,这个细节不说清楚,不少人会其实死的冤枉. 这是C#的代码. class Program { sta ...
- 配置gitlab(备忘)
已经配置好github的基础上,clone gitlab 地址git status 显示改变了的文件但是webstorm文件颜色不改变问题的解决:VCS->git-->remotes--& ...
- python djangjo完整的实现添加的实例
实现:点击添加实现模态对话框,添加数据并显示. urls.py from django.conf.urls import url from django.contrib import admin fr ...
- JMeter之BeanShell断言---获取时间戳
1.创建线程组,创建一个BeanShell Sampler,在其中编写BeanShell脚本. 2.在Jmeter中,可以利用${__time(,)}时间戳函数来获取十位的时间戳,如: vars.pu ...