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

在使用 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 >
               
< td align ="right" >
                    本地文件:
               
</ td >
               
< td >
                   
< asp:FileUpload ID ="fupFile" runat ="server" CssClass ="btn" Width ="247px" Height ="20px"
                        onkeydown
="event.returnValue=false;" onpaste ="return false" />
               
</ td >
           
</ tr >
           
< tr >
               
< td align ="right" >
                    文件列表:
               
</ td >
               
< td valign ="top" >
                   
< asp:ListBox ID ="lbxFile" runat ="server" Height ="145px" Width ="245px" CssClass ="txt" >
                   
</ asp:ListBox >
               
</ td >
           
</ tr >
           
< tr >
               
< td colspan ="5" >
                   
< asp:Button ID ="btnAdd" runat ="server" Text ="添加" OnClick ="btnAdd_Click" /> &nbsp;&nbsp;
                   
< asp:Button ID ="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;

public partial class admin_Default3 : System.Web.UI.Page
{
   
public static ArrayList files = new ArrayList();
   
protected void 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> " );
        }
    }
   
protected void btnPost_Click( object sender, EventArgs e)
    {
       
if (files.Count > 0 )
        {
           
if ( ! Directory.Exists(MapPath( " ../bodyissue/temp " )))
                Directory.CreateDirectory(MapPath(
" ../bodyissue/temp " ));


           
foreach (FileUpload fup in 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:HiddenField ID ="allFileSize" runat ="server" Value ="0" />
   
< table >
       
< tr >
           
< td align ="right" >
                本地文件:
           
</ td >
           
< td >
               
< asp:FileUpload ID ="FileUpload1" runat ="server" />
           
</ td >
       
</ tr >
       
< tr >
           
< td align ="right" >
                文件列表:
           
</ td >
           
< td >
               
< asp:ListBox ID ="lbxFile" runat ="server" Height ="145px" Width ="245px" CssClass ="txt" >
               
</ asp:ListBox >
           
</ td >
       
</ tr >
       
< tr >
           
< td colspan ="2" style ="text-align: center" >
               
< asp:Button ID ="btnAdd" runat ="server" Text ="添加文件" OnClick ="btnAdd_Click" /> &nbsp;&nbsp;
               
< asp:Button ID ="btnDelete" runat ="server" Text ="删除文件" OnClick ="btnDelete_Click" /> &nbsp;&nbsp;
               
< asp:Button ID ="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;

public partial class MultiFileUplaod : System.Web.UI.Page
{
   
private String folder;
   
protected void Page_Load( object sender, EventArgs e)
    {
        folder
=   Server.MapPath( " ~/temp " );
       
if ( ! Directory.Exists(folder))
            Directory.CreateDirectory(folder);
    }
   
protected void 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);
        }
    }
   
protected void 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> " );
    }

   
protected void 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);
                }
            }
        }
    }
}
-
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值