我正在尝试上传图像。当我从本地主机执行此操作时它可以正常工作,但是当我发布它时它会从服务器引发错误:
当我使用此代码时:
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, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs , String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System。 System.Web.HttpPostedFile.SaveAs(String filename) 在 System.Web.HttpPostedFileWrapper 的 IO.FileStream..ctor(String path, FileMode mode)。
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, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs , String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System。 IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at System.Web.HttpPostedFileWrapper.SaveAs(String filename) at xx。
我试图从我的本地主机更改权限,但没有任何效果...请给我一些建议