2012-01-16 8 views
1

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

ORIGINAL QUESTION:

私はちょうど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ドライブのサーバーにフォルダを作成しますが、私は次のエラーを取得、ウェブブラウザ上に移動すると:なぜこれが起こっている

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 

誰もが知っているの?

+0

ポスト_whole_メソッドのコードによりを交換してみてください。 'file'変数はどこで宣言しますか? – Oded

+0

上記のコードを追加しました。 – oshirowanen

+0

アップロードされたファイルコレクションから特定のItemを取得する必要があります。HttpPostedFile upload = uploads.Item [i]; – Lloyd

答えて

1

宣言されていないローカル変数ファイルを扱っているので、アイテムインデックスを使用してアップロード "Items"コレクションからHttpPostedFileを取得する必要があります。 "Default.aspxを" と "Default.aspx.cs" とともに

HttpPostedFile upload = uploads[i]; 
-1

、あなたは "Default.aspx.Designer.cs" ファイルを必要としています。 すべてのコントロールは "Designer"ファイルのスタイルで定義されています。

1

サンプルコードにバグがあるようです。この

HttpPostedFile upload = file 

HttpPostedFile upload = uploads[i] 
関連する問題