我有一个 asp.net 项目,我正在将选定的图像保存到 NewUrunler1 文件,并将图像的路径保存在我的数据库中。在本地,它可以正常工作,但自从我将项目移至我的 godaddy 主机后,保存方法会出错。这是我保存图像的代码:
protected void UploadImage()
{
try
{
HttpPostedFile yuklenecekDosya = FileUploadImage.PostedFile;
if (yuklenecekDosya != null)
{
FileInfo dosyaBilgisi = new FileInfo(yuklenecekDosya.FileName);
string yuklemeYeri = Server.MapPath("~/Images/NewUrunler1/" + dosyaBilgisi);
FileUploadImage.SaveAs(Path.Combine(yuklemeYeri));
}
}
catch (Exception e)
{
failDiv.Visible = true;
lblHata.Text = e.ToString();
}
}
当我运行它时,我从 try catch 中得到一个错误。这是错误:
System.UnauthorizedAccessException: Access to the path 'G:\PleskVhosts\ada-crm.com\httpdocs\Images\NewUrunler1\deneme.png' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at 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 CRM.UrunEkle.UploadImage()
我是 asp.net 的新手,很抱歉,如果这是一个简单的问题,但我在网上搜索但无法解决。感谢您的宝贵时间。
最佳答案
我不确定 GoDaddy,但在典型的 IIS 托管环境中,您需要确保您尝试上传到的文件夹已为与网站的 ApplicationPool 关联的用户应用写入权限。
此外,请确保路径首先存在。为此,您可以检查文件夹是否存在,如果不存在则创建它们。
关于c# - 将图像文件上传到服务器 asp.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35599892/