改变FileUpload文件上传控件的显示方式,确认后上传
一、Aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploadDemo.aspx.cs" Inherits="WebApplication1.FileUploadDemo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
.ddtop{ width:98%; height:30px; margin-top:5px; border:#EED97C 1px solid; margin-left:auto; margin-right:auto; margin-bottom:5px; background:#FFFCEB; font:"宋体"; font-size:14px; padding-left:10px; padding-top:10px;}
input{border-left:1px solid #333;border-top:1px solid #333;border-bottom:1px solid #ccc;border-right:1px solid #ccc;}
.button{ cursor:pointer; background-color:#1481E6;border:1px solid #fff;text-align:center;color:#fff;line-height:19px;}
.heigth_22{ height:22px;}
.width_70{ width:70px;}
</style>
<script type="text/javascript">
function setUpFileText(obj) {
var fileName = getFullPath(obj);
document.getElementById("upFileText").value = fileName;
} function getFullPath(obj) {
if (obj) {
// IE
if (window.navigator.userAgent.indexOf("MSIE") >= 1) {
obj.select();
return document.selection.createRange().text;
}
// Firefox
else if (window.navigator.userAgent.indexOf("Firefox") >= 1) {
if (obj.files) {
return obj.files.item(0).name;
}
}
// Chrome
else if (window.navigator.userAgent.indexOf("Chrome") >= 1) {
if (obj.files) {
return obj.files[0].name;
}
}
return obj.value;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div class="ddtop">
<div>
<span><input type="text" id="upFileText" name="upFileText" readonly="readonly" /></span>
<span style="position: absolute; z-index: 2; cursor: pointer;">
<asp:FileUpload ID="fileUpload" runat="server" CssClass="width_70 heigth_22" Style="filter: alpha(opacity=0); opacity: 0; cursor: pointer;" onchange="setUpFileText(this)" accept="image/*" />
<asp:Button ID="btnOk" runat="server" Text="确定" CssClass="button width_70 heigth_22" OnClick="btnOk_Click" />
</span>
<span style="position: absolute; z-index: 1; cursor: pointer; height: 25px;">
<input type="button" name="btnUploadFile" id="btnUploadFile" value="上傳檔案" class="button width_70 heigth_22" />
</span>
</div>
</div>
</form>
</body>
</html>
二、Aspx后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO; namespace WebApplication1
{
public partial class FileUploadDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void btnOk_Click(object sender, EventArgs e)
{
if (this.fileUpload.HasFile)
{
string fileName = this.fileUpload.PostedFile.FileName; // 客户端文件路径
string extension = System.IO.Path.GetExtension(fileName);
if (extension.ToLower() != ".jpg" && extension.ToLower() != ".png")
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "alert('只允许jpg 和 png!');", true);
return;
} string pathBase = "D:\\UploadFile";
if (!Directory.Exists(pathBase))
Directory.CreateDirectory(pathBase);
string webFilePath = Path.Combine(pathBase, fileName); // 数据库保存文件路径(相对全路径)
this.fileUpload.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "alert('上傳成功,我們會盡快進行核對!');", true);
}
}
}
}
改变FileUpload文件上传控件的显示方式,确认后上传的更多相关文章
- 改变FileUpload文件上传控件的显示方式,选择文件后自动上传
一.Aspx页面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="File ...
- asp.net web常用控件FileUpload(文件上传控件)
FileUpload控件的主要中能:向指定目录上传文件,该控件包括一个文本框和一个浏览按钮. 常用的属性:FileBytes,FileContent.FileName.HasFile.PostedFi ...
- .Net 使用文件上传控件FileUpload上传图片
例1: 来源:http://long546324.iteye.com/blog/349946 Default.aspx文档: <%@ Page Language="C#" A ...
- 对FileUpload文件上传控件的一些使用方法说明
//创建时间:2014-03-12 //创建人:幽林孤狼 //说明:FileUpload文件上传控件使用说明(只是部分)已共享学习为主 //可以上传图片,txt文档.doc,wps,还有音频文件,视屏 ...
- jquery文件上传控件 Uploadify
(转自 http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html) 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同 ...
- jquery文件上传控件 Uploadify 问题记录
Uploadify v3.2.1 首先引用下面的文件 <!--上传控件 uploadify--> <script type="text/javascript" s ...
- 因用了NeatUpload大文件上传控件而导致Nonfile portion > 4194304 bytes错误的解决方法
今天遇到一个问题,就是“NeatUpload大文件上传控件而导致Nonfile portion > 4194304 bytes错误”,百度后发现了一个解决方法,跟大家分享下: NeatUploa ...
- 给上传文件的input控件"美容"
作为一名前端程序猿呢,在工作中经常会遇到form表单这种东西.然而表单的其他input控件样式还是很好改变的.但是,唯独input类型是file的文件上传控件可能就没那么好打扮的漂亮.刚好菜鸟我最近工 ...
- ASP.NET使用文件上传控件上传图片
ASPX代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default. ...
随机推荐
- DB2隔离级别之RR/RS/CS/UR
1.RR隔离级别:在此隔离级别下. DB2会锁住全部相关的纪录. 在一个SQL语句运行期间, 全部运行此语句扫描过的纪录都会被加上对应的锁.在一个SQL语句运行期间,全部运行此语句扫描过的纪录都会 ...
- LeetCode - 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- android 常用框架
网络框架:okhttp.volley.android-async-http图片框架:Picasso.Fresco.Glide.Android-Universal-Image-Loader缓存框架:Di ...
- python数据库操作
python操作数据库应该比java更简单些,连接数据库需要有驱动,pcat就推荐自己遇到的3个模块:mysql.connector.sqlalchemy.MySQLdb
- mybatis09--自连接一对多查询
查询导师 下面的所有 老师的信息! 创建实体类 和对应的数据库 /** *导师的实体类 */ public class Teacher { private Integer id; private St ...
- 12:Css3的概念和优势
12:Css3的概念和优势 CSS3是css技术的升级版本,CSS3语言开发是朝着模块化发展的.以前的规范作为一个模块实在是太庞大而且比较复杂,所以,把它分解为一些小的模块,更多新的模块也被加入进来. ...
- 12 postgresql数据库备份和恢复
数据表结构备份与恢复 备份 1.找到postgres 安装目录,在找到bin文件夹,会看到一堆exe后缀的文件,用win+r 打开cmd,将pg_dump.exe 拖进cmd黑窗口中 2.基本语法:- ...
- PHP7.1安装xdebug
一.前言1. Xdebug 简介Xdebug 是一个开放源代码的 PHP 程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况.当前最新版本为 Xdebug 2.5.0. ...
- elk-(七)
最终架构确定为 logs--->blieb--->redis/kafka--->logstash--->es--->kibana 注意: geoip下载地址: wge ...
- vscode添加vue格式化插件
1.安装Vetur插件 2.ctrl+,打开用户设置,找到"vetur.format.defaultFormatter.html": "none",