国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

允許用戶一次上傳多個文件

2024-07-21 02:17:00
字體:
來源:轉載
供稿:網友
allowing users to upload multiple files at oncehttp://www.dotnetjunkies.com/images/trans.gif
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: http://www.dotnetjunkies.com/base/articles/2221/20020602t0201/20020602t0201.gif 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 http://www.dotnetjunkies.com/base/articles/2221/20020602t0201/20020602t0202.gif right after http://www.dotnetjunkies.com/base/articles/2221/20020602t0201/20020602t0203.gif 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.
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 垫江县| 巫溪县| 沙田区| 石泉县| 泉州市| 通许县| 台北市| 五常市| 砚山县| 安宁市| 新邵县| 和静县| 佳木斯市| 株洲市| 建始县| 会同县| 新疆| 双峰县| 池州市| 棋牌| 镇沅| 运城市| 郁南县| 高阳县| 天峨县| 延津县| 蓝田县| 赞皇县| 伊金霍洛旗| 安国市| 平遥县| 常宁市| 昌平区| 苍梧县| 枞阳县| 阜宁县| 漳州市| 望城县| 奉化市| 卫辉市| 龙井市|