首页 > 开发 > 综合 > 正文

一次同时上传多个文件

2024-07-21 02:16:51
字体:
来源:转载
供稿:网友
下面我将为大家讲解如何同时上传多个文件
我们在页面中添加5个<input type=file>上传的类型只限定在.gif和.jpg两种,并根据不同的类型放入不同的文件夹中。
我先给出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>
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() {
//文件上传时循环执行


system.web.httpfilecollection _files = system.web.httpcontext.current.request.files;

//发送给浏览器的信息
system.text.stringbuilder _message = new system.text.stringbuilder("files uploaded:<br>");

try
{
for ( system.int32 _ifile = 0; _ifile < _files.count; _ifile ++ )
{

// 检查上传文件是否为gif或jpg

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" )
{

//保存文件到指定文件夹
_postedfile.saveas(
system.web.httpcontext.current.request.mappath(
"gifs/") + _filename);
_message.append(_filename + "<br>");

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

//保存文件到指定文件夹
_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
}
}


还有vb.net代码但是我将不做解释
public class upload
    inherits system.web.ui.page

#region " web form designer generated code "

    'this call is required by the web form designer.
    <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

    end sub

    private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
        'codegen: this method call is required by the web form designer
        'do not modify it using the code editor.
        initializecomponent()
    end sub
    protected withevents linkbutton1 as system.web.ui.webcontrols.linkbutton
    protected withevents file1 as system.web.ui.htmlcontrols.htmlinputfile
    protected withevents file2 as system.web.ui.htmlcontrols.htmlinputfile
    protected withevents file3 as system.web.ui.htmlcontrols.htmlinputfile
    protected withevents file4 as system.web.ui.htmlcontrols.htmlinputfile
    protected withevents file5 as system.web.ui.htmlcontrols.htmlinputfile

#end region


    protected withevents label1 as system.web.ui.webcontrols.label

    private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
        if (me.ispostback) then me.saveimages()
    end sub

    private function saveimages() as system.boolean

        

        dim _files as system.web.httpfilecollection = system.web.httpcontext.current.request.files

        
        dim _message as new system.text.stringbuilder("files uploaded:<br>")
        dim _ifile as system.int32
        try

            for _ifile = 0 to _files.count - 1


                
                dim _postedfile as system.web.httppostedfile = _files(_ifile)
                dim _filename, _fileextension as system.string

                _filename = system.io.path.getfilename( _
                _postedfile.filename)

                _fileextension = system.io.path.getextension( _
                 _filename)

                if (_fileextension = ".gif") then


                    
                    _postedfile.saveas( _
                     system.web.httpcontext.current.request.mappath( _
                     "gifs/") + _filename)
                    _message.append(_filename + "<br>")


                elseif (_fileextension = ".jpg") then

                                    
                    _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>")

                end if

            next

            label1.text = _message.tostring()
            return true



        catch ex as system.exception


            label1.text = ex.message
            return false

        end try



    end function

end class

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表