FileUpload上传多文件出现错误的解决方法

来源:互联网 发布:上海东华网络教育 编辑:程序博客网 时间:2024/06/07 23:58

在使用 public static ArrayList files 变量保存临时上传的文件时,当文件比较大时,会出现“无法访问已关闭的文件”错误,网上也有很多这样的问题,但都没有解决办法。在配置文件中增加

XML/XHTML 代码
<httpRuntime executionTimeout="90" maxRequestLength="2097151" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>

(属性“maxRequestLength”值必须在 0-2097151 范围内。)一行之后,可以解决部分问题,但也不能彻底解决。出现这样的问题的代码是这样写的:

ASPX 代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="admin_Default3"%>
<!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>
       
<table>
           
<tr>
               
<tdalign="right">
                    本地文件:
               
</td>
               
<td>
                   
<asp:FileUploadID="fupFile" runat="server" CssClass="btn" Width="247px" Height="20px"
                        onkeydown
="event.returnValue=false;" onpaste="return false"/>
               
</td>
           
</tr>
           
<tr>
               
<tdalign="right">
                    文件列表:
               
</td>
               
<tdvalign="top">
                   
<asp:ListBoxID="lbxFile" runat="server" Height="145px" Width="245px" CssClass="txt">
                   
</asp:ListBox>
               
</td>
           
</tr>
           
<tr>
               
<tdcolspan="5">
                   
<asp:ButtonID="btnAdd" runat="server" Text="添加" OnClick="btnAdd_Click"/>&nbsp;&nbsp;
                   
<asp:ButtonID="btnPost" runat="server" Text="上传" OnClick="btnPost_Click"/>
               
</td>
           
</tr>
       
</table>
   
</div>
   
</form>
</body>
</html>
C# 代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Collections;
using System.IO;

publicpartialclass admin_Default3 : System.Web.UI.Page
{
   
publicstatic ArrayList files=new ArrayList();
   
protectedvoid btnAdd_Click(object sender, EventArgs e)
    {
       
if (fupFile.HasFile)
        {

            ListItem item
=new ListItem();
            item.Value
= item.Text= fupFile.PostedFile.FileName;
           
if (!lbxFile.Items.Contains(item))
            {
                lbxFile.Items.Add(item);
                files.Add(fupFile);
            }
           
else
                Page.ClientScript.RegisterClientScriptBlock(
typeof(string),"", @"<script>alert('不能添加已经添加过的文件!')</script>");
        }
    }
   
protectedvoid btnPost_Click(object sender, EventArgs e)
    {
       
if (files.Count>0)
        {
           
if (!Directory.Exists(MapPath("../bodyissue/temp")))
                Directory.CreateDirectory(MapPath(
"../bodyissue/temp"));


           
foreach (FileUpload fupin files)
            {
               
if (fup.HasFile)

                    fup.SaveAs(MapPath(
"../bodyissue/temp")+"/"+ fup.FileName);//无法访问已关闭的文件

            }
            Page.ClientScript.RegisterClientScriptBlock(
typeof(string),"", @"<script>alert('上传成功!')</script>");


        }
    }
}

要实现类似的功能,其实完全没有必要使用 static 变量,使用 static 变量,也会导致一些问题,因为 .NET 中 static 变量是所有线程共同使用的。下面的这个方法的代码,就解决这个问题。

ASPX 代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultiFileUpload.aspx.cs"
    Inherits
="MultiFileUplaod"%>
<!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">
   
<asp:HiddenFieldID="allFileSize" runat="server" Value="0"/>
   
<table>
       
<tr>
           
<tdalign="right">
                本地文件:
           
</td>
           
<td>
               
<asp:FileUploadID="FileUpload1" runat="server"/>
           
</td>
       
</tr>
       
<tr>
           
<tdalign="right">
                文件列表:
           
</td>
           
<td>
               
<asp:ListBoxID="lbxFile" runat="server" Height="145px" Width="245px" CssClass="txt">
               
</asp:ListBox>
           
</td>
       
</tr>
       
<tr>
           
<tdcolspan="2" style="text-align: center">
               
<asp:ButtonID="btnAdd" runat="server" Text="添加文件" OnClick="btnAdd_Click"/>&nbsp;&nbsp;
               
<asp:ButtonID="btnDelete" runat="server" Text="删除文件" OnClick="btnDelete_Click"/>&nbsp;&nbsp;
               
<asp:ButtonID="btnPost" runat="server" Text="完成上传" OnClick="btnPost_Click"/>
           
</td>
       
</tr>
   
</table>
   
</form>
</body>
</html>
C# 代码
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.IO;

publicpartialclass MultiFileUplaod : System.Web.UI.Page
{
   
private String folder;
   
protectedvoid Page_Load(object sender, EventArgs e)
    {
        folder
=  Server.MapPath("~/temp");
       
if (!Directory.Exists(folder))
            Directory.CreateDirectory(folder);
    }
   
protectedvoid btnAdd_Click(object sender, EventArgs e)
    {
       
if (FileUpload1.HasFile)
        {
            String newFileName
= folder+"/"+ Guid.NewGuid().ToString() + Path.GetExtension(FileUpload1.FileName);
           
int totalFileSize= Int32.Parse(allFileSize.Value);
           
int fileSize= FileUpload1.PostedFile.ContentLength;     
           
//此处也可以限制单个文件的大小
            if (totalFileSize+ fileSize >1024*1024)
            {
                Page.ClientScript.RegisterClientScriptBlock(
typeof(string),"", @"<script>alert('总上传的文件超过了大小设置  1024 * 1024 !')</script>");
               
return;
            }           
            FileUpload1.SaveAs(newFileName);
            ListItem item
=new ListItem();
            item.Text
= FileUpload1.FileName;
            item.Value
= newFileName;
           
for (int i=0; i< lbxFile.Items.Count; i++)
            {
               
if (lbxFile.Items[i].Text.Equals(FileUpload1.FileName, StringComparison.InvariantCultureIgnoreCase))
                {
                    Page.ClientScript.RegisterClientScriptBlock(
typeof(string),"", @"<script>alert('不能添加已经添加过的文件!')</script>");
                   
return;
                }
            }
            totalFileSize
+= fileSize;
            allFileSize.Value
= totalFileSize.ToString();
            lbxFile.Items.Add(item);
        }
    }
   
protectedvoid btnPost_Click(object sender, EventArgs e)
    {
       
//对上传的文件进行进一步处理,或者退出弹出窗口等操作
        for (int i= lbxFile.Items.Count-1; i>-1; i--)
        {
            lbxFile.Items.Remove(lbxFile.Items[i]);
        }
        Page.ClientScript.RegisterClientScriptBlock(
typeof(string),"", @"<script>alert('上传成功!')</script>");
    }

   
protectedvoid btnDelete_Click(object sender, EventArgs e)
    {
       
for (int i= lbxFile.Items.Count-1; i>-1; i--)
        {
           
if (lbxFile.Items[i].Selected)
            {
                String value
= lbxFile.Items[i].Value;
                lbxFile.Items.Remove(lbxFile.Items[i]);
               
if (File.Exists(value))
                {
                    File.Delete(value);
                }
            }
        }
    }
}
-