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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 优雅小猪

xp远程桌面登录 - 优雅小猪 xmlhttp调用后台 - 优雅小猪 w3c的web标准校验工具 最小高度的实现 - 优雅小猪 - 博客园 div左右排版 - 优雅小猪 - 博客园 网页上运行代码和复制代码 - 优雅小猪 div vertical-align不起作用解决办法 - 优雅小猪 - 博客园 parentNode、parentElement,childNodes、children - 优雅小猪 web后台运行任务(zz) 两个div,一个左右排列不换行(zz:http://www.cnblogs.com/circlesport/archive/2007/03/15/676261.html) - 优雅小猪 控制web页面的某块是否显示出来 - 优雅小猪 SVN的建立配置和简单使用 用<%if()%>控制页面某些部分是否可见 - 优雅小猪 - 博客园 placeholder LiteralControl从后台生成前台控件 - 优雅小猪 - 博客园 鼠标移入移出某个元素,元素背景变化 获得汉字的拼音首字母以及整个拼音 javascript同名函数 错误配置 Title换行 - 优雅小猪
asp.net上传多个文件 - 优雅小猪 - 博客园
优雅小猪 · 2010-03-24 · via 博客园 - 优雅小猪

上传一个或者多个文件

需要把form写成这样:

 <form method="post" id="form1" onsubmit="/*postForm();return false;*/" action="Default.aspx" style="margin:0px;" runat="server" enctype="multipart/form-data">
     选择上传图片:
     <br />
     <span id="FileList"><input type="file"  size="50" name="File"  /></span>
    <br />

     <a href='#' onclick="addFile(10)" ><font color='blue'>增加更多图片</font></a>(每次最多上传10张图片)
     <br/>

             <asp:Button ID="btnSave" runat="server" Text="开始上传" height='30px' onclick="btnSave_Click"></asp:Button>
        <input type="hidden" id="DelID" name="DelID" />
      </form>

<script type="text/javascript" language="javascript">

    function addFile(max)
    {
        var file = document.getElementsByName("File");
        if (file.length < max) {
            var filebutton = '<br /><input type="file" size="50" name="File"  />';
            document.getElementById('FileList').insertAdjacentHTML("beforeEnd", filebutton);

         
        }
        else {
          alert('每次最多上传' + max + '张图片')
        }
    }
</script>

其中, document.getElementById('FileList').insertAdjacentHTML("beforeEnd", filebutton);

也可以写成:
            document.getElementById('FileList').innerHTML += filebutton

注意不能少enctype="multipart/form-data",否则后台取不到文件

后台:

int uploadcount = 0;

            try
            {
                ///获取上载文件的列表
                HttpFileCollection fileList = HttpContext.Current.Request.Files;
                if (fileList == null) return;

                ///上载文件列表中的每一个文件
                for (int i = 0; i < fileList.Count; i++)
                {   ///获取当前上载的文件
                    HttpPostedFile postedFile = fileList[i];
                    if (postedFile == null)
                    {
                        continue;
                    }

                    ///获取上载文件的文件名称
                    String fileExt = (System.IO.Path.GetExtension(postedFile.FileName)).ToString().ToLower();
                    String strImageName = "pictures\\" + System.Guid.NewGuid().ToString();

                    if (string.IsNullOrEmpty(fileExt) == true)
                    {
                        continue;
                    }

                    //保存上传图片到服务器
                    postedFile.SaveAs(Server.MapPath(strImageName + fileExt));

                    (new Picture()).UploadPicture("", strImageName + fileExt);
                    uploadcount++;

                }

                if (uploadcount>0)
                {
                    Response.Redirect("UploadSucceed.aspx?succeed=true");
                }
                else
                {
                    Response.Write("<script language=javascript>alert(\'系统提示:请选择文件!\');</script>");
                }


            }
            catch (Exception ex)
            {
                Response.Write("<script language=javascript>alert(\'系统警告:上传图片失败!\');</script>");

            }

如果就上传一个文件,则file元素加上runat='server',也可以直接在后台访问:

            ////获取图片文件扩展名
            //String fileExt = (System.IO.Path.GetExtension(fileImage.PostedFile.FileName)).ToString().ToLower();
            //String strImageName = "pictures\\" + System.Guid.NewGuid().ToString();
            //try
            //{
            //   if (fileImage.PostedFile.ContentLength != 0) //判断选取对话框选取的文件长度是否为0
            //    {
            //        //保存上传图片到服务器
            //        fileImage.PostedFile.SaveAs(Server.MapPath(strImageName + fileExt));

                    //        (new Picture()).UploadPicture("", strImageName + fileExt);
            //        Response.Redirect("UploadSucceed.aspx?succeed=true");
            //    }
            //    else
            //    {
            //        Response.Write("<script language=javascript>alert(\'系统提示:请选择文件!\');</script>");
            //    }

            //}
            //catch (Exception ex)
            //{
            //    Response.Write("<script language=javascript>alert(\'系统警告:上传图片失败!\');</script>");

                       //}