程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> FileUpload上傳多文件出現錯誤的解決方法

FileUpload上傳多文件出現錯誤的解決方法

編輯:.NET實例教程

在使用 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))
            {
             &nbsp;  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" >
                <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&g
t;
    </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;
 &n
bsp;          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);
                }
            }
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved