0

イメージをアップロードしようとしています。ローカルホストからアップロードすると正常に動作しますが、公開するとサーバーからエラーがスローされます。

このコードを使用する場合:

public string ImagePath(HttpPostedFileBase imgfile)
        {
            var path = "";
            // code for saving the image file to a physical location.
            var fileName = Path.GetFileName(imgfile.FileName);


            path = Path.Combine(HttpContext.Server.MapPath("~/Images/Sections/Developer/ClientLogo"), fileName);

            string fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(imgfile.FileName);
            int iteration = 1;
            while (System.IO.File.Exists((path)))
            {
                fileName = string.Concat(fileNameWithoutExtension, "-", iteration, System.IO.Path.GetExtension(imgfile.FileName));

                path = Path.Combine(HttpContext.Server.MapPath("~/Images/Sections/Developer/ClientLogo"), fileName);
                iteration++;
            }
            imgfile.SaveAs(path);
            // prepare a relative path to be stored in the database and used to display later on.
            path = Url.Content(Path.Combine("~/Images/Sections/Developer/ClientLogo", fileName));
            return path;
        }

エラーは

System.UnauthorizedAccessException: パス 'D:\InetPub\vhosts\xx.com\httpdocs\Images\Sections\Developer\ClientLogo\circle-small-empty.18x18.png' へのアクセスが拒否されました。System.IO.__Error.WinIOError (Int32 errorCode、文字列の多分フルパス) で System.IO.FileStream.Init (文字列パス、FileMode モード、FileAccess アクセス、Int32 権限、ブール値の useRights、FileShare 共有、Int32 bufferSize、FileOptions オプション、SECURITY_ATTRIBUTES secAttrs System.IO.FileStream..ctor(文字列パス、FileMode モード、FileAccess アクセス、FileShare 共有、Int32 bufferSize、FileOptions オプション、文字列 msgPath、ブール値 bFromProxy) でSystem.Web.HttpPostedFileWrapper の System.Web.HttpPostedFile.SaveAs(String filename) の IO.FileStream..ctor(String パス、FileMode モード)。

And when I use Server.MapPath instead of HttpContext.Server.MapPath it throw different error:

エラーは次のとおりです。

System.IO.DirectoryNotFoundException: パス 'D:\InetPub\vhosts\xx.com\httpdocs\Images\Sections\Developer\ClientLogo\demo.png' の一部が見つかりませんでした。System.IO.__Error.WinIOError (Int32 errorCode、文字列の多分フルパス) で System.IO.FileStream.Init (文字列パス、FileMode モード、FileAccess アクセス、Int32 権限、ブール値の useRights、FileShare 共有、Int32 bufferSize、FileOptions オプション、SECURITY_ATTRIBUTES secAttrs System.IO.FileStream..ctor(文字列パス、FileMode モード、FileAccess アクセス、FileShare 共有、Int32 bufferSize、FileOptions オプション、文字列 msgPath、ブール値 bFromProxy) でIO.FileStream..ctor (文字列パス、FileMode モード) で System.Web.HttpPostedFile.SaveAs(文字列ファイル名) で System.Web.HttpPostedFileWrapper.SaveAs(文字列ファイル名) で xx。

ローカルホストから許可を変更しようとしましたが、何も機能していません...何か提案してください

4

2 に答える 2

2

Web アプリケーションには、画像を保存しようとしている場所への書き込み権限がありません。これは多くの場合、すべてのアップロードが保存されるフォルダーを指すエントリを web.config に追加することで処理されます

<appSettings>
  ...
  <add key="uploadPath" value="C:\Uploads"/>
  ...
</appSettings>

次に、コードでその構成エントリを読み取って、画像が保存されるパスを特定します。

....
string path = ConfigurationManager.AppSettings["uploadPath"];
string filePath = Path.Combine(path, fileName);
....

次に、ファイルをこのディレクトリに保存するには、Web アプリケーションを実行しているユーザーがそのディレクトリに書き込み権限を持つように、ディレクトリに権限を設定する必要があります。これにより、Web アプリケーションからそのフォルダーにファイルを書き込むことができます。

このように、開発者はファイルの保存先を指定しません。システム管理者は、ファイルの保存先と、Web アプリケーションをサポートするために必要なアクセス許可の両方を決定で​​きます。

于 2013-02-12T21:26:18.500 に答える