1

UPDATE 1:

Here is the full code behind from the downloaded example, unedited:

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.";
            }
        }
    }
}

ORIGINAL QUESTION:

I've just downloaded a "file uploading" example from www.asp.net:

Link to tutorial: http://www.asp.net/web-forms/videos/how-do-i/how-do-i-multiple-file-uploads-in-aspnet-2 Link to download: http://download.microsoft.com/download/8/6/9/869ff08a-1e39-4bab-a303-f7dcedc52427/CS-ASP-MultiFileUpload-CS.zip

and copied the files to my webserver after extracting them.

When I navigate to http://server/uploader/Default.aspx

It creates the folder in the servers c drive successfully, but on the web browser I get the following error:

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 

Anyone know why this is happening?

4
  • Post the code of the whole method. Where do you declare the file variable? Commented Jan 16, 2012 at 11:05
  • Added full code behind above. Commented Jan 16, 2012 at 11:15
  • Probably you should be getting the specific Item from the uploaded files collection HttpPostedFile upload = uploads.Item[i]; Commented Jan 16, 2012 at 11:15
  • HttpPostedFile upload = file; , where is file coming from it has no reference fix it Commented Jan 16, 2012 at 11:16

3 Answers 3

1

You are addressing an undeclared local variable file, you should be retrieving the HttpPostedFile from the uploads "Items" collection using the Item index;

HttpPostedFile upload = uploads[i]; 
Sign up to request clarification or add additional context in comments.

Comments

1

It seems a bug on the sample code. Try replacing this

HttpPostedFile upload = file

by

HttpPostedFile upload = uploads[i]

Comments

-1

Along with "Default.aspx" and "Default.aspx.cs", you need "Default.aspx.Designer.cs" File. As all the controls are defined with its style in "Designer" file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.