piperzero 发表于 2013-1-22 22:34:27

FileUpload上传多文件时出现“无法访问已关闭的文件”错误的解决方法

在使用 public static ArrayList files 变量保存临时上传的文件时,当文件比较大时,会出现“无法访问已关闭的文件”错误,网上也有很多这样的问题,但都没有解决办法。在配置文件中增加
<httpRuntime executionTimeout="90" maxRequestLength="2097151" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>
(属性“maxRequestLength”值必须在 0-2097151 范围内。)一行之后,可以解决部分问题,但也不能彻底解决。出现这样的问题的代码是这样写的:
<%@ 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="添加"/>                  <asp:Button ID="btnPost" runat="server" Text="上传"/>                </td>            </tr>      </table>    </div>    </form></body></html>
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), "", @"<mce:script type="text/javascript"><!--alert('不能添加已经添加过的文件!')// --></mce: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), "", @"<mce:script type="text/javascript"><!--alert('上传成功!')// --></mce:script>");      }    }}
要实现类似的功能,其实完全没有必要使用 static 变量,使用 static 变量,也会导致一些问题,因为 .NET 中 static 变量是所有线程共同使用的。下面的这个方法的代码,就解决这个问题。
<%@ 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" mce_style="text-align: center">                <asp:Button ID="btnAdd" runat="server" Text="添加文件"/>                <asp:Button ID="btnDelete" runat="server" Text="删除文件"/>                <asp:Button ID="btnPost" runat="server" Text="完成上传"/>            </td>      </tr>    </table>    </form></body></html>
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), "", @"<mce:script type="text/javascript"><!--alert('总上传的文件超过了大小设置1024 * 1024 !')// --></mce: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.Text.Equals(FileUpload1.FileName, StringComparison.InvariantCultureIgnoreCase))                {                  Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", @"<mce:script type="text/javascript"><!--alert('不能添加已经添加过的文件!')// --></mce: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);      }      Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", @"<mce:script type="text/javascript"><!--alert('上传成功!')// --></mce:script>");    }    protected void btnDelete_Click(object sender, EventArgs e)    {      for (int i = lbxFile.Items.Count - 1; i > -1; i--)      {            if (lbxFile.Items.Selected)            {                String value = lbxFile.Items.Value;                lbxFile.Items.Remove(lbxFile.Items);                if (File.Exists(value))                {                  File.Delete(value);                }            }      }    }}
页: [1]
查看完整版本: FileUpload上传多文件时出现“无法访问已关闭的文件”错误的解决方法