改变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. ...
随机推荐
- tensorflow c/c++库使用方法
tensorflow目前支持最好的语言还是python,但大部分服务都用C++ or Java开发,一般采用动态链接库(.so)方式调用算法,因此tensorflow的c/c++ API还是有必要熟悉 ...
- FatFs文件系统的移植
FatFs 的底层可以写一次命令,读写多个扇区.FatFs的设计的读写的思想就很好,小块的数据,我就经过Buffer来存储,大块的数据,我就直接进行存取,那样速度,效率高了很多,看图: FatFs文件 ...
- AT&T汇编指令
GAS中每个操作都是有一个字符的后缀,表明操作数的大小. C声明 GAS后缀 大小(字节) char b 1 short w 2 (unsigned) int / long / char* l 4 f ...
- windows环境下redis启动加到服务中
前置条件: 1.命令运行在redis-server.exe目录下 2.cmd命令 安装命令: redis-server.exe --service-install redis.windows.co ...
- 用SUMIF对超15位的代码进行条件求和,出错了,原因是....
用SUMIF对超15位的代码进行条件求和,出错了,原因是.... 2017-10-29 23:01 一.问题 有读者朋友问: 用SUMIF进行条件求和时,如果统计的条件是超15位的代码,就会出错,比如 ...
- 一、K3 WISE 实施顾问教程《进度1-谈谈实施顾问》
1.为什么要开这门课? 从自身的原因说起,在我从开发顾问转岗做实施顾问.售后服务顾问时,我就定下了我做顾问的目标. 第一个核心目标,帮助成千上万的企业客户促进他们商业的成功!第二个目标,成为最顶级的顾 ...
- js 注意
1.如果想要动态加清除浮动的代码,可以这样做: document.getElementById("mainBody").innerHTML += "<div sty ...
- FTP连接服务器总报错的问题解决
在使用宝塔面板的时候,我在使用FTP的时候,总有这样的问题,FTP老是连接不上,花了两个小时左右的时间总算找到问题:端口问题. 首先一般的FTP端口是:21,22,我这里就改成:9527 了 然后回到 ...
- web开发——入门篇(上)
作为一名IT届的后生,当初也经历过懵懂无知的实习期,对那种无力感深有体会.在这,希望能用我这几年的开发经验,让各位即将踏入或者刚刚踏入web开发领域的新人们少走些弯路.鉴于这是入门篇,下面我就从零为大 ...
- PHP base64数据与图片的互相转换
1.解析base64数据成图片 The problem is that data:image/bmp;base64, is included in the encoded contents. This ...