dotnet下上传文件方便多了,问题是怎么写代码才可以上传多个文件???(100分)

  • 主题发起人 主题发起人 MichaelLee
  • 开始时间 开始时间
M

MichaelLee

Unregistered / Unconfirmed
GUEST, unregistred user!
//单个文件上传代码,测试通过
<Script Language="C#" Runat="Server">
public void Page_Load(Object src,EventArgs e)
{
//设置HtmlInputFile控件的属性
UploadFile.MaxLength = 30;
UploadFile.Size = 20;
}
public voiddo
Upload(Object src,EventArgs e)
{
HttpPostedFile hpf = UploadFile.PostedFile;
FileName.Text = hpf.FileName;
FileSize.Text = hpf.ContentLength.ToString();

//取得文件名(不含路径)
char[] de = {'//'};
string[] AFilename = hpf.FileName.Split(de);
string strFilename = AFilename[AFilename.Length-1];
rname.Text = strFilename;
hpf.SaveAs(Server.MapPath(".")+"//"+strFilename);
}
</script>
<html>
<head>
<title>上传单个文件</title>
</head>
<body>
<form Enctype="multipart/form-data" runat="server">
<b>上传单个文件示例</b>
<hr>
请选择文件上传:<input type="file" id="UploadFile" runat="server"><br>
<input type="submit" value="上传" OnServerClick="DoUpload" runat="server">
<hr>
上传文件名:<asp:Label id="FileName" Text="尚未上传" runat="server" /><br>
上传文件大小:<asp:Label id="FileSize" Text="尚未上传" runat="server" /><br>
真实文件名:<asp:Label id="rname" Text="尚未上传" runat="server" />
</form>
</body>
</html>
 
用Request.File可以上传N个:
<Script language="C#" runat="server">
public voiddo
Upload(Object src,EventArgs e){
int FileCount=Request.Files.Count;
string strFileName;
string[] AFileName;
char[] de={'//'};

for(int i=0;i<FileCount;i++){
HttpPostedFile hpf=Request.Files;
if (hpf.FileName=="") continue;
AFileName=hpf.FileName.Split(de);
strFileName=AFileName[AFileName.Length-1];
hpf.SaveAs(Server.MapPath("/")+"//FileUp//"+strFileName);
}
Body.Attributes["OnLoad"]=Server.HtmlEncode("window.close();");
}
</Script>
<html>
<body id="Body" runat="server">
<form enctype="multipart/form-data" runat="server">
选择文件:<input type="file" id="UploadFile1" runat="server" ><br>
选择文件:<input type="file" id="UploadFile2" runat="server" >
<input type="submit" value="上传" onserverclick="DoUpload" runat="server" />
</form>
</body>
</html
 
测试不通过[:(]
 
把路径改成你的上载路径。
 
记得把上传目录的权限改一下,否则是不能写入的!
 
后退
顶部