单纯的事件与获取

<input type="file" name="file" id="fileUpload">
<img id="preview" src=""/>

jQuery

$("#fileUpload").change(function () {
console.log($("#fileUpload")[0].files);
});

原生JavaScript

  var popImg = document.getElementById("fileUpload")   ----------
popImg.onchange = function() {
const file = document.getElementById('fileUpload');  // 获取 input(只是演示可以使用外面的 popImg)
const fileObj = file.files[0];  // 获取选中的文件信息
console.log(fileObj)
const windowURL = window.URL || window.webkitURL; // 兼容操作
const img = document.getElementById('preview');  // 获取img元素
if(file && fileObj) {  // 判断是否为空 null
const dataURl = windowURL.createObjectURL(fileObj);
img.setAttribute('src',dataURl);
}
}

URL.createObjectURL()用法(https://blog.csdn.net/qq_39258552/article/details/84133770)

===============

1.如何实现file上传文件,预览效果

参考(https://blog.csdn.net/xiaohu12685/article/details/80328022)

<input id="pop_file" type="file" accept=".jpg,.jpeg,.png" v-on:change="uploadFile($event)" name="fileTrans" ref="file" value="" />
<img id="preview" src=""/>

js.

uploadFile:function(ev){
var that = this;
const file = document.getElementById('pop_file');
const fileObj = file.files[0];
const windowURL = window.URL || window.webkitURL;
const img = document.getElementById('preview');
if(file && fileObj) {
const dataURl = windowURL.createObjectURL(fileObj);
img.setAttribute('src',dataURl);
}
}

2.获取到file里的文件,使用异步的请求实现局部刷新的效果

<form @submit.prevent="addbanner()">
    <input id="pop_file" type="file" accept=".jpg,.jpeg,.png" v-on:change="uploadFile($event)" name="fileTrans" ref="file" value="" />
    <input class="pop_but" type="submit" value="提交"/>
</form>

js.

addbanner: function(ev) {
    var oFiles = document.getElementById("pop_file").files;
    var params = new FormData();
    params.append('file',oFiles[0]);
    axios({
        method: 'post',
        url: 'http://请求路径/admin/BannerApi/actionBannerSave',
        headers: {
            'Content-type': 'application/x-www-form-urlencoded;charset=UTf-8'
        },
        data: params
    })
    .then(function(response) {
        console.log(response)
    })
}

如果是ajax请求的话

var oFiles = document.getElementById("pop_file").files;
var params = new FormData();
params.append('file',oFiles[0]);
$.ajax({
type:'post',
url:'http://api.tianshuai.com.cn/admin/BannerApi/actionBannerSave',
data:params,
cache: false,
contentType: false,
processData: false,
success:function(data){
console.log(data)
}
})

如何获取input,file里的文件,实现预览效果,并传给后端?的更多相关文章

  1. input type=file 选择图片并且实现预览效果的实例

    为大家带来一篇input type=file 选择图片并且实现预览效果的实例. 通过<input />标签,给它指定type类型为file,可提供文件上传: accept:可选择上传类型, ...

  2. input type=file 选择图片并且实现预览效果

    通过<input />标签,给它指定type类型为file,可提供文件上传: accept:可选择上传类型,如:只要传图片,且不限制图片格式,为image/*: multiple:规定是否 ...

  3. input file样式修改,图片预览删除功能

    本篇对input file进行了修改,改成自己需要的样式,类似验证身份上传身份证图片的功能. 效果图如下: 这里主要展示上传预览图片功能,对于删除功能的html及css写的比较粗糙,对于想要精细表现这 ...

  4. JS input file 转base64 JS图片预览

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. input file 在开发中遇到的问题 类似ajax form表单提交 input file中的文件

    最近在做项目的过程中遇到个问题,在这里做个记录防止日后忘记 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为 ...

  6. ajax form表单提交 input file中的文件

    ajax form表单提交 input file中的文件 现今的主流浏览器由于ajax提交form表单无法把文件类型数据提交到后台,供后台处理,可是开发中由于某些原因又不得不用ajax提交文件, 为了 ...

  7. 在另一个文本框显示input file选择的文件名字

    javascript 获取文件域 (type=file) 的完整路径一直是很麻烦的问题,问题主要出在一些浏览器基于安全性考虑而不能正常获取到文件域中选中图片的决对路径,尤其一些基于webkit的浏览器 ...

  8. 使用input:file控件在微信内置浏览器上传文件返回未显示选择的文件

    使用input:file控件在微信内置浏览器上传文件返回未显示选择的文件 原来的写法: <input type="file" accept="image/x-png ...

  9. Java实现文件的预览

    最近项目需要用到文件的预览功能,就开始在网上收集资料,学习了几种文件预览的方法.我集成到我项目内测试的有以下三种,最后使用的是第三种: 直接使用别人提供的服务 例如:office web 365 使用 ...

随机推荐

  1. VUE项目Eslint报错

    前言:eslint很恶心的一个地方:你是否被各种语法报错一个标点符号,一个空格,一个回车......各种报错折磨着你! 加上编辑器 VS Code 的自动格式化稳稳的和Eslint冲突报错. 对此,我 ...

  2. Cesium专栏-地形开挖2-任意多边形开挖(附源码下载)

    “任意多边形地形开挖” 是“地形开挖”的补充篇,在这节里,我们介绍关于如何使用任意多边形对地形进行开挖,同时,由于有不少小伙伴也咨询了关于“地形开挖”篇后序内容中的填充地形的效果,之前没放出来,是想让 ...

  3. SVN清理失败(clean up)或者(lock)问题进入死循环最终解决方案

    解决方法: step1: 到 sqlite官网 (http://www.sqlite.org/download.html) 下载 sqlite3.exe step2: 将下载到的 sqlite3.ex ...

  4. 【30天自制操作系统】day02:寄存器和Makefile

    基本寄存器 AX(accumulator):累加寄存器 CX(counter):计数寄存器 DX(data):数据寄存器 BX(base):基址寄存器 SP(stack pointer):栈指针寄存器 ...

  5. javascript中的对象拷贝

    js中的数据类型 在介绍javascript中的对象的拷贝之前,我先介绍一个基础的东西,javascript中的数据类型. 我们做前端的应该都知到在es6 之前,javascript中的数据类型Boo ...

  6. SpringCloud Zuul2.X网关实现服务熔断降级(复制即用)

    版本: <properties> <spring-boot.version>.RELEASE</spring-boot.version> <spring-cl ...

  7. How to: Implement a Custom Base Persistent Class 如何:实现自定义持久化基类

    XAF ships with the Business Class Library that contains a number of persistent classes ready for use ...

  8. python的学习大纲

    python基础部分 函数 初识函数 函数进阶 装饰器函数 迭代器和生成器 内置函数和匿名函数 递归函数 常用模块 常用模块 模块和包 面向对象 初识面向对象 面向对象进阶 网络编程 网络编程 并发编 ...

  9. asp.net允许跨域配置web.config

    <configuration> <system.webServer> <modules> <add name="CultureAwareHttpMo ...

  10. java程序猿工具

    1.做为一个程序员打字最重要    指法打字练习软件:http://home.bdqn.cn/thread-11142-1-1.html 2.JDK必不可少    JDK6.X下载       JDK ...