改变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. ...
随机推荐
- Webpack+Vue+ES6 前端组件化开发mobile-multi-page应用实战总结和踩坑
本文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws 一.写在前面 项目上线有一段时间了,一个基于webpack+vue+ES6的手机端多页面应用 ...
- LINUX下从mysql文件导出后标题合并
这两天在做数据导出,真实折磨死了,记录下来.导出的格式是csv. 由于我们的数据量比较大,导出到excel时,几百万上千万行的时候用程序去写入肯定是不行,所以自然就想到了mysql的outfile功能 ...
- 三维计算机视觉 — 中层次视觉 — Point Pair Feature
机器人视觉中有一项重要人物就是从场景中提取物体的位置,姿态.图像处理算法借助Deep Learning 的东风已经在图像的物体标记领域耍的飞起了.而从三维场景中提取物体还有待研究.目前已有的思路是先提 ...
- work table a year
近10个月~ 回顾一下最近写代码和博客~ 4.5.6月份刷OJ~ 7月中旬入职滴滴,几乎放弃了博客... GitHub提交统计,主要集中在准备机试,刷了九度OJ~ GitLab in didi~ 很 ...
- proxy_set_header Host 所引发的凶案
背景介绍:新搭建了一套测试环境.slb为2.2.2.2,由于应用的特殊性,需要走 test.aaa.com.cn 域名,而该域名在老的测试服务器1.1.1.1有两个不能迁移的服务也在使用,故想出对策, ...
- gitlab-ci + k8s 之gitlab-ci(一)
目前常用的持续集成工具主要是jenkins与gitlab-ci ,我已在另一博文中详细记录了jenkins部署过程(其中包括gitlab的搭建),此篇介绍gitlab-ci的使用. 背景介绍 GitL ...
- 给table加边框的样式
<style> .tb { width: 1600px; text-align: center; border-collapse: collapse; } .tb tr td { bord ...
- 不偏移的天地图地图服务-ArcGIS版
地图偏移和纠偏是使用在线电子地图不可避免的话题.研究不深入,暂且分享一种已纠偏的地图服务. 服务地址 直接放点干货: 影像地图: http://t0.tianditu.com/cia_w/esri/w ...
- 向comboboxEdit中动态添加数据库中保存的用户自定义单位制的名称
if (radioGroup1.SelectedIndex == 2) { bool _Flag = true; sm.SetLciVisible(lciDelete, _Flag); sm.SetL ...
- linux考试题改错
符号链接和硬链接有什么区别? 改:符号链接存储文件路径,可以指向不同分区文件,源文件删除后失效. 改:硬链接指向文件索引节点,仅能指向同一分区文件,源文件删除后可以访问. 请描述文件和目录9位权限位的 ...