1、input file 样式不能满足需求

 <input type="file" value="浏览" />

IE8效果图:    Firefox效果图: Chrome效果图:  

2、input file上传按钮美化

css:

.file{
position: relative;
background-color: #b32b1b;
border: 1px solid #ddd;
width: 68px;
height: 25px;
display: inline-block;
text-decoration: none;
text-indent:;
line-height: 25px;
font-size: 14px;
color: #fff;
margin: 0 auto;
cursor: pointer;
text-align: center;
border: none;
border-radius: 3px;
}
.file input{
position: absolute;
top:;
left: -2px;
opacity:;
width: 10px;
}

html:

<div>
<span>选择文件:</span><input id="txt_filePath" type="text" readonly="readonly"/>
<a class="file"><input id="btnfile" name="btnfile" type="file"/>浏览</a>
</div>

美化后的效果图:

3、ajax+ashx实现上传功能

引入文件:jquery-1.11.3.js  ajaxfileupload.js

js:

 $(function () {
//选择文件
$(".file").on("change", "input[type='file']", function () {
var filePath = $(this).val();
//设置上传文件类型
if (filePath.indexOf("xls") != -1 || filePath.indexOf("xlsx") != -1) {
//上传文件
$.ajaxFileUpload({
url: 'ASHX/FileHandler.ashx',
secureuri: false,
fileElementId: 'btnfile',
dataType: 'json',
success: function (data, status) {
//获取上传文件路径
$("#txt_filePath").val(data.filenewname);
alert("文件上传成功!");
},
error: function (data, status, e) {
alert(e);
}
});
} else {
alert("请选择正确的文件格式!");
//清空上传路径
$("#txt_filePath").val("");
return false;
}
});
})
FileHandler.ashx
public class FileHandler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string msg = string.Empty;
string error = string.Empty;
string result = string.Empty;
string filePath = string.Empty;
string fileNewName = string.Empty;
//这里只能用<input type="file" />才能有效果,因为服务器控件是HttpInputFile类型
HttpFileCollection files = context.Request.Files;
if (files.Count > )
{
//设置文件名
fileNewName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "_" + System.IO.Path.GetFileName(files[].FileName);
//保存文件
files[].SaveAs(context.Server.MapPath("~/Upload/"+fileNewName));
msg = "文件上传成功!";
result = "{msg:'" + msg + "',filenewname:'" + fileNewName + "'}";
}
else
{
error = "文件上传失败!";
result = "{ error:'" + error + "'}";
}
context.Response.Write(result);
context.Response.End();
} public bool IsReusable {
get {
return false;
}
} }

实现一个简单上传功能

ajax+ashx 完美实现input file上传文件的更多相关文章

  1. input file 上传文件

    面试的时候遇到一个问题,要求手写的方式上传文件. 本来觉得很简单,但是结果怎么也成功不了. 前台: <form ID="form1" action="AcceptF ...

  2. 在HTML5的 input:file 上传文件类型控制 遇到的问题

    1.input:file 属性的介绍  先瞅代码吧 <form> <input type="file" name="pic" accept=& ...

  3. input file上传文件

    如何使用input[type='file']来上传文件呢? html: //angular<input type="file" (change)="fileChan ...

  4. 巧妙利用label标签实现input file上传文件自定义样式

    提到上传文件,一般会想到用input file属性来实现,简单便捷,一行代码即可    但input file原生提供的默认样式大多情况下都不符合需求,且在不同浏览器上呈现的样式也不尽相同   我们往 ...

  5. 使用input file上传文件中onChange事件只触发一次问题

    每次上传文件的时候,都会将当前的文件路径保存至$event.target.value中,当第二次选择文件时,由于两次$event.target.value相同,所以不会触发change事件. 解决方案 ...

  6. input file上传文件扩展名限制

    方法一(不推荐使用):用jS获获取扩展名进行验证: <script type="text/javascript" charset="utf-8"> ...

  7. angular input file 上传文件

    <body > <div ng-controller="fileCtrl"> <form ng-submit="submit(obj)&qu ...

  8. input file上传文件弹出框的默认格式设置

    我们使用html的input 标签type="flie"时,如何设置默认可选的文件格式 <input id="doc_file" type="f ...

  9. input:file上传文件类型(记录)

    imput 属性有以下几种: 1.type:input类型这就不多说了2.accept:表示可以选择的文件类型,多个类型用英文逗号分开,常用的类型见下表. <input id="fil ...

随机推荐

  1. HDU1465 第六周L题(错排组合数)

    L - 计数,排列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descrip ...

  2. hdu3949

    XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. 重构后的程序:通过rsync命令抓取日志文件

    push.sh #!/bin/bash function push() { local ip=$ local user=$ local password=$ local path=$ local lo ...

  4. Solr4.8.0源码分析(13)之LuceneCore的索引修复

    Solr4.8.0源码分析(13)之LuceneCore的索引修复 题记:今天在公司研究elasticsearch,突然看到一篇博客说elasticsearch具有索引修复功能,顿感好奇,于是点进去看 ...

  5. 在CentOS6上使用YUM安装Mysql5.5.x

    1.安装MySQL 5.5.x的yum源: rpm -Uvh http://repo.webtatic.com/yum/centos/5/latest.rpm 2.安装MySQL客户端的支持包: yu ...

  6. 转:Top 10 Algorithms for Coding Interview

    The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...

  7. C语言学习--链表

    #include "node.h" #include<stdio.h> #include<stdlib.h> //typedef struct _node ...

  8. poj Cash Machine

    http://poj.org/problem?id=1276 #include<cstdio> #include<cstring> #include<cmath> ...

  9. GDI+画图类Graphics的使用

    一:基础定义 #region 定义线尾.线头为箭头.字体和笔刷 Pen p = );//定义画笔 蓝色,宽度为1(坐标显示颜色) p.EndCap = LineCap.ArrowAnchor;//定义 ...

  10. Hbase 计数器

    Hbase计数器可以用于统计用户数,点击量等信息 基本操作 可以使用incr操作计数器,incr语法格式如下: incr '<table>', '<row>', '<co ...