允许用户一次上传多个文件

上传

Allowing Users to Upload Multiple Files at Once
Summary: This article demonstrates how to allow users to upload multiple files from thier computer (the client) to your server. Specifically, this article will demonstrate how to set up a page that has 5 HtmlInputFile controls (rendered as <INPUT TYPE='FILE'> ) where a user can choose 5 images to upload. Only .jpg and .gif extensions will be accepted on the server and each one will be saved to a different directory::so any image that has the extension .jpg will be saved to the jpgs direcory and .gif will be saved to the gifs directory and everything else isn't saved at all. Before we get into the code I want to quickly go over some things that may or may not be new to you. First, System.Web.HttpFileCollection: this class "Provides access to and organizes files uploaded by the client" - essentially, it contains a collection of HttpPostedFile classes and is access through the System.Web.HttpConext.Current.Response.Files property. The second thing used is some static methods of the System.IO.Path class: The GetFileName and GetExtension methods are used. The GetFileName method will retrieve the file name and file extension from a path. The GetExtension method does exactly what it sounds like it should do. It gets the file extension from a file. Let's take a look at the code. The first code listing is the WebForm code and the second is the WebForm's code behind file. Note: for this to work properly you will have to create two sub-directories (gifs, jpgs) Upload.aspx <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML>
<HEAD>
<title>::: UPLOAD SAMPLE ::: </title>
</HEAD>
<body>
<center>
<form id="UPLOAD" method="post" runat="server" enctype="multipart/form-data">
<h3>Multiple File Upload Example</h3>
<P>
<INPUT type="file" runat="server" size="50" ID="File1" NAME="File1"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File2" NAME="File2"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File3" NAME="File3"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File4" NAME="File4"></P>
<P>
<INPUT type="file" runat="server" size="50" ID="File5" NAME="File5"></P>
<P><STRONG>::  </STRONG>
<asp:LinkButton id="LinkButton1" runat="server" Font-Names="Verdana" Font-Bold="True" Font-Size="XX-Small">Upload Images</asp:LinkButton>  <STRONG>::
</STRONG>  <A href="JavaScript:document.forms[0].reset()" id="LinkButton2" style="FONT-WEIGHT:bold;FONT-SIZE:xx-small;FONT-FAMILY:verdana">
Reset Form</A> <STRONG>::</STRONG></P>
<P>
<asp:Label id="Label1" runat="server" Font-Names="verdana" Font-Bold="True" Font-Size="XX-Small" Width="400px" BorderStyle="None" BorderColor="White"></asp:Label></P>
<P> </P>
</form>
</center>
</body>
</HTML> As usual the WebForm is pretty boiler plate. 5 HtmlInputFile objects and some buttons for submitting the form and reseting the form. When this page is rendered you'll receive something like the following: Update.aspx.cs namespace HowTos.MultipleImageUpdate
{
public class UPLOAD : System.Web.UI.Page
{

#region User Defined Code

protected System.Web.UI.WebControls.LinkButton LinkButton1;

protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(System.Object sender, System.EventArgs e)
{
if ( this.IsPostBack )
this.SaveImages();
}

private System.Boolean SaveImages() {
//loop through the files uploaded

System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;

//Message to the user
System.Text.StringBuilder _message = new System.Text.StringBuilder("Files Uploaded:<br>");

try
{
for ( System.Int32 _iFile = 0; _iFile < _files.Count; _iFile ++ )
{

// Check to make sure the uploaded file is a jpg or gif

System.Web.HttpPostedFile _postedFile = _files[_iFile];
System.String _fileName, _fileExtension;

_fileName = System.IO.Path.GetFileName(
_postedFile.FileName);

_fileExtension = System.IO.Path.GetExtension(
_fileName);

if ( _fileExtension == ".gif" )
{

//Save File to the proper directory
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
"gifs/") + _fileName);
_message.Append(_fileName + "<BR>");

}
else if ( _fileExtension == ".jpg" )
{

//Save File to the proper directory
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
"jpgs/") + _fileName);
_message.Append(_fileName + "<BR>");

}
else {

_message.Append(_fileName + " <font color=\"red\">failed!! Only .gif and .jpg images allowed!</font> <BR>");

}

}

Label1.Text = _message.ToString();
return true;
}
catch ( System.Exception Ex )
{

Label1.Text = Ex.Message ;
return false;

}

}
#endregion

#region Web Form Designer generated code
override protected void OnInit(System.EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
Well just be looking at the SaveImages method. The first thing that's done is a new HttpFileCollection object and a new StringBuilder object is created - the StringBuilder is used to create a message to the user. Then we loop through the HttpFileCollection object and verify whether a gif or jpeg was uploaded - if so it is saved to the proper directory and if not then nothing happens. Take a look at the following images. The first is the page right before it is posted to the server and the second is right after. Notice that there is both jpg, gif, and psd extensions being uploaded. Right Before Right After Notice that because the .psd file wasn't valid an error message was displayed to the user. Conclusion: When you want to enable users to upload multiple files use the HttpFileCollection to access the HttpPostedFile's uploaded.

时间: 2024-11-04 17:35:42

允许用户一次上传多个文件的相关文章

防止用户上传产生无效文件源码

上传|上传 经过近日来的调试,狂人于4月12日写的一篇日志"用户上传产生无效文件的解决思路"代码部分已经完成.欢迎批评指正,以便改进. [FileName]:upload.asp[Code]:<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%><!--#i nclude virtual="upload.inc"--><% If Request.ServerVariable

紧急求助:linux vsftpd虚拟用户能上传、下载文件,没有删除权限

问题描述 紧急求助:linux vsftpd虚拟用户能上传.下载文件,没有删除权限 SELinux设置为disable了,文件属主是virtural(virtual是虚拟用户所在的本地用户) 解决方案 写个脚本将文件扫描到就赋予所有权限呢 解决方案二: 把write_enable=yes前面的注释取消试试

微软正融合OneDrive 与Xbox Music,用户将能上传Xbo Music

摘要: 微软正将旗下在线文件存储服务 OneDrive 和自有音乐流媒体平台 Xbox Music 相融合,从而允许用户将 Xbox Music Store 中没有的音乐上传至前者,并能在任何运行 Xbox Music 的设备上播放这些存储 微软正将旗下在线文件存储服务 OneDrive 和自有音乐流媒体平台 Xbox Music 相融合,从而允许用户将 Xbox Music Store 中没有的音乐上传至前者,并能在任何运行 Xbox Music 的设备上播放这些存储在云端的音乐. 此前One

微软正融合OneDrive 与Xbox Music,用户将能上传Xbox Music

摘要: 微软正将旗下在线文件存储服务 OneDrive 和自有音乐流媒体平台 Xbox Music 相融合,从而允许用户将 Xbox Music Store 中没有的音乐上传至前者,并能在任何运行 Xbox Music 的设备上播放这些存储 微软正将旗下在线文件存储服务 OneDrive 和自有音乐流媒体平台 Xbox Music 相融合,从而允许用户将 Xbox Music Store 中没有的音乐上传至前者,并能在任何运行 Xbox Music 的设备上播放这些存储在云端的音乐. 此前One

上传多个文件的PHP脚本

脚本|上传 译者注:本文的原名是<Creating a Multi-File Upload Script in PHP>.我个个觉得这文章写得一般,代码也不是非常专业,但是它比较短,而且一时间也找不到好一点的文章,就把这个译过来了.其实PHP手册里也有一节是说多文件上传的,大家可以对比对比.文章内有的字句译出来不太顺眼,所以按照原意作了一定量的修改.本人水平有限,还望各位高手多指点. 导言     作为一个PHP的程序员,我曾遇到过这么一个客户,他需要一个可以同时上传多个文件的表单.因此,一天

Linux使用SecureCRT上传和下载文件教程

  Linux使用SecureCRT上传和下载文件教程:         我们在日常工作中,经常会需要上传或者下载一些文件,Linux用户也不例外.那么在Linux系统中,有什么比较好用的上传下载工具呢?今天小编就教大家如何使用SecureCRT上传或下载文件. 具体操作步骤: 1.需要上传或者下载,需要使用rz和sz命令.如果linux上没有这两个命令工具,则需要先安装.可以使用yum安装.运行命令yum install lrzsz. 2.安装完成后就可以使用rz命令上传本地的文件到linux

aspx页面,pc端可上传160mb的文件,手机访问却连56mb的都上传失败,求解释

问题描述 aspx页面,pc端可上传160mb的文件,手机访问却连56mb的都上传失败,求解释主要并不是报错黄页,而是直接无法找到页面,我尝试将附件的名称由中文改成英文字母abc.zip还是不行 解决方案 本帖最后由 mailaidedt 于 2015-05-21 09:20:02 编辑解决方案二:参考:解决方案三:如果"pc端可上传160mb的文件"正确,那么就跟服务器端的配置没有关系了.你可以先把手机在wifi环境下.访问局域网里的asp.net应用网站测试一下.如果上传情况很好,

SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)

  林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程免费下载 一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+standard.jar 整个相关的包如下: 整个工程目录如下: 二.配置web.xml和S

上传多个文件的PHP脚本_php技巧

译者注:本文的原名是<Creating a Multi-File Upload Script in PHP>.我个个觉得这文章写得一般,代码也不是非常专业,但是它比较短,而且一时间也找不到好一点的文章,就把这个译过来了.其实PHP手册里也有一节是说多文件上传的,大家可以对比对比.文章内有的字句译出来不太顺眼,所以按照原意作了一定量的修改.本人水平有限,还望各位高手多指点.  导言 作为一个PHP的程序员,我曾遇到过这么一个客户,他需要一个可以同时上传多个文件的表单.因此,一天夜里我就用了一个小