イメージをアップロードしようとしています。ローカルホストからアップロードすると正常に動作しますが、公開するとサーバーからエラーがスローされます。
このコードを使用する場合:
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。
ローカルホストから許可を変更しようとしましたが、何も機能していません...何か提案してください