获取(名称'文件'在当前上下文中不存在)消息

时间:2012-01-16 11:03:56

标签: c# asp.net .net-3.5 webforms asp.net-3.5

更新1:

以下是未经编辑的下载示例的完整代码:

using System;
using System.IO;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String UpPath;
        UpPath = "C:\\UploadedUserFiles";

        if (!Directory.Exists(UpPath))
        {
            Directory.CreateDirectory("C:\\UploadedUserFiles\\");
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        HttpFileCollection uploads = HttpContext.Current.Request.Files;
        for (int i = 0; i < uploads.Count; i++)
        {
            HttpPostedFile upload = file;

            if (upload.ContentLength == 0)
                continue;

            string c = System.IO.Path.GetFileName(upload.FileName); // We don't need the path, just the name.

        try
            {
            upload.SaveAs("C:\\UploadedUserFiles\\" + c);
            Span1.InnerHtml = "Upload(s) Successful.";
            }
        catch(Exception Exp)
            {
                Span1.InnerHtml = "Upload(s) FAILED.";
            }
        }
    }
}

原始问题:

我刚刚下载了#34;文件上传&#34;来自www.asp.net的例子:

指向教程的链接:http://www.asp.net/web-forms/videos/how-do-i/how-do-i-multiple-file-uploads-in-aspnet-2 链接下载:http://download.microsoft.com/download/8/6/9/869ff08a-1e39-4bab-a303-f7dcedc52427/CS-ASP-MultiFileUpload-CS.zip

并在提取文件后将文件复制到我的网络服务器。

当我导航到http://server/uploader/Default.aspx

它成功地在服务器c驱动器中创建了文件夹,但在Web浏览器上我收到以下错误:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0103: The name 'file' does not exist in the current context

Source Error:



Line 29:         for (int i = 0; i < uploads.Count; i++)
Line 30:         {
Line 31:             HttpPostedFile upload = file;
Line 32: 
Line 33:             if (upload.ContentLength == 0)


Source File: c:\Inetpub\wwwroot\uploader\Default.aspx.cs    Line: 31 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3623; ASP.NET Version:2.0.50727.3618 

任何人都知道为什么会这样吗?

3 个答案:

答案 0 :(得分:1)

您正在处理未声明的本地变量文件,您应该使用Item索引从上传“Items”集合中检索HttpPostedFile;

HttpPostedFile upload = uploads[i]; 

答案 1 :(得分:1)

这似乎是示例代码的一个错误。尝试替换此

HttpPostedFile upload = file

通过

HttpPostedFile upload = uploads[i]

答案 2 :(得分:-1)

除了“Default.aspx”和“Default.aspx.cs”之外,还需要“Default.aspx.Designer.cs”文件。 因为所有控件都在“Designer”文件中定义了它的样式。