惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
M
MIT News - Artificial intelligence
U
Unit 42
博客园 - 三生石上(FineUI控件)
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
I
InfoQ
WordPress大学
WordPress大学
H
Help Net Security
D
Docker
B
Blog
腾讯CDC
A
About on SuperTechFans
Recent Announcements
Recent Announcements
雷峰网
雷峰网
有赞技术团队
有赞技术团队
C
Check Point Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - melody&bobo

C#加密解密 端口数据库连接 ANSI, UNICODE,UTF8编码的区别 调用系统API打印图片文字 windows2003安全设置 图片与字节数组相互转换的方法 jQuery Ajax 方法调用 Asp.Net WebService 的详细例子 使用instantclient_11_2和PL/SQL Developer工具包连接oracle 11g远程数据库 查看SQL 语句执行性能 SSIS 获取时间表达式 获得时间段之间每月的最后一天 ADSL拨号获得不同IP地址 C#(.Net) 解决Informix中文乱码问题 SQL SPLIT() 方法实现 JS获取URL参数值 datatable操作集合 nginx实现网站负载均衡测试实例(windows下IIS做负载实测) Web Application Stress Tool(WAS,Web应用负载测试工具)详细说明 Base64 加密字符串和文件
表单提交文件 - melody&bobo - 博客园
melody&a · 2010-09-17 · via 博客园 - melody&bobo

代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>无标题页</title>
    
<script type="text/javascript">
    
function upfile() {
    
var form = document.dform;
    
// check the filename first
    var file = document.getElementById("ifile").value;
    
var pos = file.lastIndexOf('.');
    
var ext = file.substring(pos + 1).toLowerCase();
    
if (ext != 'jpg' && ext != 'gif' && ext != 'png')
    {
        alert(
'错误:您选择上传必须是 jpg/gif/png 文件!');
        
return;
    }
    
// replace form options
    var action = form.action;
    
var enctype = form.enctype;
    
var target = form.target;
    form.action 
="default3.aspx";
    form.enctype 
= 'multipart/form-data';
    
if (typeof form.encoding != 'undefined') form.encoding = 'multipart/form-data';
    form.submit();
    }
    
</script>
</head>
<body>
    
<form id="dform" name="dform"  method="post"  runat="Server">
    
<div>
        
<input id="ifile" name="ifile"  type="file"  onchange="upfile()"  />
        
</div>
    
</form>
</body>
</html>

代码

    protected void Page_Load(object sender, EventArgs e)
    {
        
if (Request.Files.Count > 0)
        {
            
string path = Server.MapPath("uploadfile/");
            HttpPostedFile postfile 
= Request.Files[0];
            
if (postfile.ContentLength > 0)
            {
                
string name = DateTime.Now.ToString("yyMMddHHmmssfff"+ postfile.FileName;
                postfile.SaveAs(path 
+ name);
                Page.ClientScript.RegisterStartupScript(
this.GetType(), "js""<script>printimage('uploadfile/" + name + "')</script>");
            }
        }
    }