.net中自带的上传工具虽然好用,可是上传不了大文件。现在有一个免费的可以用,Bestcomy.Web.Controls.Upload 组件支持多文件上传。
使用这个组件必须要先去Web.config里注册一下:
<httpModules>
<add name="UploadModule" type="Bestcomy.Web.Controls.Upload.UploadModule,Bestcomy.Web.Controls.Upload" />
</httpModules>
下面这一段是来限止上传文件大小的,放在<configuration>标签里
<aspnetUploadSettings>
<!--
Key Name: lisenceKey
Valid Value: Purchased lisence key from the control author.
-->
<add key="lisenceKey" value="Lisence key purchase from www.aspnetupload.net"/>
<!--
Key Name: maxRequestLength
Valid Value: KBytes size of maximum upload file length to accept
-->
<add key="maxRequestLength" value="2048000"/>
</aspnetUploadSettings>
前台文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Debug="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>
后台文件:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Bestcomy.Web.Controls.Upload;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AspnetUpload upldr = new AspnetUpload();
upldr.RegisterModelessProgressBar(this.Button1);//调用组件上传时所用到的上传按钮
string fpath = Path.Combine(Request.PhysicalApplicationPath, "temp");//设置上传目录
if (!Directory.Exists(fpath))//检查上传目录是否存在,如果不存在就建立这个目录
Directory.CreateDirectory(fpath);
upldr.set_UploadFolder(fpath); //设置给件的上传目录
}
protected void Button1_Click(object sender, EventArgs e)
{
string fpath = Path.Combine(Request.PhysicalApplicationPath, "temp");
//获取界面内的上传组件
UploadFileCollection files = AspnetUpload.GetUploadFiles("FileUpload1");
//遍历上传组件
foreach (UploadFile file in files)
{
//判断上传组件内是否已有文件
if (file != null)
file.SaveAs(Path.Combine(fpath, Path.GetFileName(file.get_FileName())));
}
}
}
特别要注意要在CS文件里注册一下:
using System.IO;
using Bestcomy.Web.Controls.Upload;
另外,我有个问题搞不懂,用了这个组件后:就无法使用FileUpload1.PostedFile.FileName检查上传文件了,那么用这个组件上传大文件时该怎么检查文件大小和文件类型。如果有谁知道,告诉我一声,谢谢,QQ:41611039