一、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">
.button{ cursor:pointer; background-color:#1481E6;border:1px solid #fff;text-align:center;height: 25px;color:#fff;line-height:19px;}
</style>
<script type="text/javascript">
function uploadFile(filePath) {
if (filePath.length > 0) {
__doPostBack('btnUploadFile', '');
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div style="position: absolute; z-index: 2; cursor: pointer;">
<asp:FileUpload ID="fileUpload" runat="server" Style="filter: alpha(opacity=0); opacity: 0; cursor: pointer;" Width="70px" Height="25px" onchange="uploadFile(this.value)" accept="image/*" />
</div>
<div style="position: absolute; z-index: 1; cursor: pointer; height: 25px;">
<input type="button" name="btnUploadFile" id="btnUploadFile" runat="server" value="上傳新檔案" class="button" />
</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)
{
btnUploadFile.ServerClick += new EventHandler(btnUploadFile_ServerClick);
} void btnUploadFile_ServerClick(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);
}
}
}
}

参考:http://www.cnblogs.com/Charles2008/archive/2008/07/20/1247084.html

改变FileUpload文件上传控件的显示方式,选择文件后自动上传的更多相关文章

  1. 改变FileUpload文件上传控件的显示方式,确认后上传

    一.Aspx页面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="File ...

  2. 对FileUpload文件上传控件的一些使用方法说明

    //创建时间:2014-03-12 //创建人:幽林孤狼 //说明:FileUpload文件上传控件使用说明(只是部分)已共享学习为主 //可以上传图片,txt文档.doc,wps,还有音频文件,视屏 ...

  3. jquery文件上传控件 Uploadify

    (转自 http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html) 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同 ...

  4. jquery文件上传控件 Uploadify 问题记录

    Uploadify v3.2.1 首先引用下面的文件 <!--上传控件 uploadify--> <script type="text/javascript" s ...

  5. 因用了NeatUpload大文件上传控件而导致Nonfile portion > 4194304 bytes错误的解决方法

    今天遇到一个问题,就是“NeatUpload大文件上传控件而导致Nonfile portion > 4194304 bytes错误”,百度后发现了一个解决方法,跟大家分享下: NeatUploa ...

  6. jquery文件上传控件 Uploadify 可以和ajax交互

    http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html  原网址 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同 ...

  7. jquery文件上传控件 Uploadify(转)

    原文:http://www.cnblogs.com/mofish/archive/2012/11/30/2796698.html 基于jquery的文件上传控件,支持ajax无刷新上传,多个文件同时上 ...

  8. 使用Uploadify(UploadiFive)多文件上传控件遇到的坑

    最近项目中需要实现多文件上传功能,于是结合需求最终选择了Uploadify这一款控件来实现.相比其他控件,Uploadify具有简洁的界面,功能API基本可以解决大多数需求,又是基于jquery的,配 ...

  9. 在WebBrowser中通过模拟键盘鼠标操控网页中的文件上传控件(转)

    引言 这两天沉迷了Google SketchUp,刚刚玩够,一时兴起,研究了一下WebBrowser. 我在<WebBrowser控件使用技巧分享>一文中曾谈到过“我现在可以通过WebBr ...

随机推荐

  1. 【深入Java虚拟机】二 类加载与双亲委派

    https://blog.csdn.net/zhangliangzi/article/details/51338291  -参考 双亲委派过程:当一个类加载器收到类加载任务时,立即将任务委派给它的父类 ...

  2. 八、Sql Server 基础培训《进度8-查询多种写法》(实际操作)

    知识点: 假设学生表.班级表.年级表 学生表(student) 内码 学生姓名 班级内码 001 张三 1002 002 李四 1002 003 王五 1003 004 钱六 1001 班级表(cla ...

  3. ubuntu14.04 terminator字体挤在一起问题

    字体挤在一起:在ubuntu下请选择mono后缀的字体就可以了 右键—>首选项—>profile—>general—>字体设置成ubuntu mono 或Free mono

  4. 给新手学习Java的建议

    有很多的朋友都在问我一个同样的问题:新手应该如何能学好Java.我做了一个简单的总结,分享给大家: 1-信念:无论你是选择JAVA,C,C#,C++....还是其他的语言编程,信念是第一位,只有相信自 ...

  5. ux.form.field.GridDate 支持快速选择日期的日期控件

    效果如图,亲测6.2.1版本可用 /** *支持快速选择日期的日期控件 */ Ext.define('ux.form.field.GridDate', { extend: 'Ext.form.fiel ...

  6. CentOS 7.0关闭服务器的防火墙服务命令

    1.直接关闭防火墙systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.service #禁止firewall ...

  7. Nestjs 获取cookie

    Docs yarn add cookie-parser main.ts import { NestFactory } from '@nestjs/core'; import { AppModule } ...

  8. vue中使用html2canvas及解决html2canvas截屏图片模糊问题

    最近在项目中用到了html2canvas插件,遇到的一些坑写下来,与大家共勉. html2canvas  官方网站http://html2canvas.hertzen.com/index.html 这 ...

  9. java基础学习总结——流

    一.JAVA流式输入/输出原理

  10. js 时分秒与秒数的转换

    1. 时间戳 格式化为 时分秒(00:00:00) /** * 时间秒数格式化 * @param s 时间戳(单位:秒) * @returns {*} 格式化后的时分秒 */ var sec_to_t ...