选择显示字体大小

asp.net 2.0 多文件上传小经验

   想实现任意多个文件上传的功能,点击一次按钮可以添加一个文件上传框,以前在网络硬盘上看到过。javascript我知道怎么实现任意添加上传文件控件,问题是添加的是html控件,我不懂怎么让服务器端可以获取文件。

    于是上google搜索“asp.net 多文件上传”,还真找到一篇文件,标题为《在asp.net中实现多文件上传》,文章里面是vb.net实现的,功能和我要的一模一样,我主要是要看服务器端怎么获取客户端上传的文件,看了文中的代码,原来这么简单,system.web.httpcontext.current.request.files就包含客户端浏览器上传的文件了,我用c#写了一段简单的代码,原本以为应该可以了,结果出乎意料上传3个图片system.web.httpcontext.current.request.files返回的文件格式还是0个。

    不知道什么原因,看看代码,这么简单不可能些错啊,再看看google搜索结果里的另外几篇文章,发现我看的第一篇不是原做,作者的网站上原作的实例有两个版本,一个是vb.net一个是c#的,现在我不用自己写了,复制原文的代码到本地,运行,果然可以啊,那我写的代码怎么不行?反复比对我的代码和文章中代码的区别,试了几个地方,最后发现和其他地方都没有关系,原因出在<form id="form1" runat="server" enctype="multipart/form-data"> 的enctype属性上,vs 2005建的页面里没有这个属性,而文章实例里有,我后来加上enctype="multipart/form-data"后system.web.httpcontext.current.request.files就能z正常获取文件个数了。

    大概是vs 2003建的页面默认有这个属性吧,否则这么重要的属性作者应该会在文章中提到的。

参考:
《在asp.net中实现多文件上传》

我做的试验代码:

<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="demo._default" %>

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script language="javascript">
function addfile()
{
 var str = '<input type="file" size="50" name="file">'
 document.getelementbyid('myfile').insertadjacenthtml("beforeend",str)
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>untitled page</title>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
           <input type="button" value="增加(add)" onclick="addfile()">
          <input onclick="this.form.reset()" type="button" value="重置(reset)">
          <asp:button runat="server" text="上传" id="upload" onclick="upload_click1" ></asp:button>
    <div id="myfile">
         <input type="file" name="file" />
    </div>
    </form>
</body>
</html>

using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

namespace demo
{
    public partial class _default : system.web.ui.page
    {
        protected void page_load(object sender, eventargs e)
        {


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons