ASP.NET的单文件上传使用控件
<asp:FileUpload ID="upmess" runat="server" Width="248px" />
后台代码
try{
if (upmess.HasFile == true) //判断是否选择了上传文件
{
bool fileOk = false;
string fileExtension = System.IO.Path.GetExtension(upmess.FileName).ToLower();
string[] allowExtension = { ".doc", ".txt", ".wps", ".xls", ".zip", ".pdf", ".rar" }; // 上传文件的类型限制
for (int i = 0; i < allowExtension.Length; i++)
{
if (fileExtension == allowExtension[i])
{
fileOk = true;
}
}
if (fileOk)
{
int filesize = upmess.PostedFile.ContentLength;
if (filesize < 15360000) /*上传文件的大小限制*/
{
string path = Server.MapPath("~");
Random r=new Random ();
r.Next (1,100);
string mark = r.Next(1, 1000).ToString ();
filename ="[" +mark+"]"+ upmess.FileName;
upmess.PostedFile.SaveAs(path + "\\view\\"+filedizi+"\\" + filename); //保存上传文件到指定的目录
}
获取上传文件时
Response.Redirect(@"" + path + "/" + filename);
上传文件的大小,相应时间,多用户同时上传文件的个数可在webConfig中配置
<system.web>
<httpRuntime executionTimeout="600" maxRequestLength="15360000" //文件大小限制
useFullyQualifiedRedirectUrl="false" minFreeThreads="8"
minLocalRequestFreeThreads="4" //多用户同时上传文件个数:4-8
appRequestQueueLimit="100" //相应时间100ms
enableVersionHeader="true"/>
<httpHandlers>