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
誰もが知っているの?
ポスト_whole_メソッドのコードによりを交換してみてください。 'file'変数はどこで宣言しますか? – Oded
上記のコードを追加しました。 – oshirowanen
アップロードされたファイルコレクションから特定のItemを取得する必要があります。HttpPostedFile upload = uploads.Item [i]; – Lloyd