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. 弹出层iframe链接设置

    jQuery 比较方便就是创建删除了,所以创建一个弹出层就是当点击div的时候创建一个新的div利用固定位fixed(与浏览器窗口有关)和z-index覆盖body 并利用opacity设置其透明度产 ...

  2. jquery easy ui 学习 (6) basic validatebox

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. php 数组 类对象 值传递 引用传递 区别

    一般的数据类型(int, float, bool)不做这方面的解说了 这里详细介绍一下数组和的类的对象作为参数进行值传递的区别 数组值传递 实例代码: <?php function main() ...

  4. 给AVS添加描述(how to add a description to a video)

    UPDATE you might need edit few files. 1. add the input field to the tpl file: /templates/frontend/yo ...

  5. UVA - 12627 Erratic Expansion 奇怪的气球膨胀 (分治)

    紫书例题p245 Piotr found a magical box in heaven. Its magic power is that if you place any red balloon i ...

  6. java的占位符

    java占位符的类型: 常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.fo ...

  7. stringstream复用【原创】

    stringstream ss("123"); int i=0; ss>>i;   ss.str("");        ----清空内容 ss.c ...

  8. 重新定义malloc和free 防止内存泄漏

    1, 定义供应用程序使用的头文件//libmem.h#ifndef _LIBMEM_H_#define _LIBMEM_H_ //声明自定义malloc及free函数extern void *my_m ...

  9. 一步一步学习SignalR进行实时通信_7_非代理

    原文:一步一步学习SignalR进行实时通信_7_非代理 一步一步学习SignalR进行实时通信\_7_非代理 SignalR 一步一步学习SignalR进行实时通信_7_非代理 前言 代理与非代理 ...

  10. 【HDOJ】2440 Watch out the Animal

    刚开始学随机算法,凸包+模拟退火. /* 2440 */ #include <iostream> #include <cstdio> #include <cstring& ...