正在将图像文件上传到服务器 asp.net

Uploading image file to server asp.net

我有一个 asp.net 项目,我正在将选定的图像保存到 NewU运行ler1 文件,并将图像的路径保存在我的数据库中。在本地,它可以正常工作,但自从我将项目移至我的 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 关联的用户应用写入权限.

另外,首先确保路径存在。您可以检查文件夹是否存在,如果不存在则创建它们。

这与 ASP.NET 和基本 windows 安全无关。有人将您的网站设置为 运行,该用户无权写入该网站。

从安全的角度来看,这是有道理的。现在打电话给您的主机支持并询问他们 - 如何更改此设置以便用户网站进程 运行ning 可以 read/write 进入文件夹 ;)

你的代码没有问题,Godaddy主机访问有问题。 Goto > Virtual Directory > Directory Access Permission 并将 Full Control 设置为 Application pool group

https://www.godaddy.com/help/set-directory-permissions-windows-6481 这是在 godaddy 上设置写入权限的手册。希望对您有所帮助。在您的代码将起作用的文件夹上向您的应用程序授予写入权限