ajax+ashx 完美实现input file上传文件
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上传文件的更多相关文章
- input file 上传文件
面试的时候遇到一个问题,要求手写的方式上传文件. 本来觉得很简单,但是结果怎么也成功不了. 前台: <form ID="form1" action="AcceptF ...
- 在HTML5的 input:file 上传文件类型控制 遇到的问题
1.input:file 属性的介绍 先瞅代码吧 <form> <input type="file" name="pic" accept=& ...
- input file上传文件
如何使用input[type='file']来上传文件呢? html: //angular<input type="file" (change)="fileChan ...
- 巧妙利用label标签实现input file上传文件自定义样式
提到上传文件,一般会想到用input file属性来实现,简单便捷,一行代码即可 但input file原生提供的默认样式大多情况下都不符合需求,且在不同浏览器上呈现的样式也不尽相同 我们往 ...
- 使用input file上传文件中onChange事件只触发一次问题
每次上传文件的时候,都会将当前的文件路径保存至$event.target.value中,当第二次选择文件时,由于两次$event.target.value相同,所以不会触发change事件. 解决方案 ...
- input file上传文件扩展名限制
方法一(不推荐使用):用jS获获取扩展名进行验证: <script type="text/javascript" charset="utf-8"> ...
- angular input file 上传文件
<body > <div ng-controller="fileCtrl"> <form ng-submit="submit(obj)&qu ...
- input file上传文件弹出框的默认格式设置
我们使用html的input 标签type="flie"时,如何设置默认可选的文件格式 <input id="doc_file" type="f ...
- input:file上传文件类型(记录)
imput 属性有以下几种: 1.type:input类型这就不多说了2.accept:表示可以选择的文件类型,多个类型用英文逗号分开,常用的类型见下表. <input id="fil ...
随机推荐
- Day5 双层装饰器、字符串格式化、生成器、迭代器、递归
双层装饰器实现用户登录和权限认证 #!/usr/bin/env python# -*- coding: utf-8 -*-# Author: WangHuafeng USER_INFO = {} de ...
- weekly review
鉴于某位昔日工作在我身边的大师一直在写review,所以为了能靠近大师,我也要开始写review了. 无名师曾经说过,想要成为大师的话,要先找到一个大师,然后追随大师,再然后与大师通行,之后成为大师, ...
- Shortest Prefixes
poj2001:http://poj.org/problem?id=2001 题意:给你一些单词,然后让你寻找每个单词的一个前缀,这个前缀能够唯一表示这个单词,并且是最短的. 题解:直接用trie树来 ...
- cf C. Counting Kangaroos is Fun
http://codeforces.com/contest/373/problem/C 贪心,先排序,然后从n/2位置倒着找每一个可不可以放到别的袋鼠内. #include <cstdio> ...
- h.264 Bi-Predictive Motion Search
在做B帧的运动预测时,有两组参考图像列表(refList0, refList1),需要进行分别前向预测.后向预测.传统的预测方式是: 对refList0进行前向预测,得到最佳前向ref与mv. 对re ...
- PBOC规范研究
一.ISO14443协议和PBOC关于CID的约定 看过协议的人其实都明白,RATS命令中参数字节的低半字节是CID,期中,CID不能为15. ISO14443协议中要求当RATS命令的CID等于0时 ...
- 【HDOJ】1063 Exponentiation
这道题目莫名其妙的wa,又莫名其妙的过了. import java.util.Scanner; import java.math.BigDecimal; public class Main { pub ...
- 【HDOJ】1274 展开字符串
栈的应用,需要注意括号前可能没有数字的情况. #include <cstdio> #include <cstring> #include <cstdlib> #in ...
- 【动态规划】XMU 1029 矩阵链乘法
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1029 题目大意: 题同乘法难题.给n+1个数,头尾不能动,中间的数可取出,取出时代价是 ...
- python3、selenium、autoit3,通过flash控件上传文件
autoit.au3 #include <Constants.au3> WinWait(); //暂停执行脚本,直到上传对话框出现 WinActive("打开") Wi ...