C# ASP.NET MVC文件上载在本地生成上工作,部署后不工作

C# ASP.NET MVC文件上载在本地生成上工作,部署后不工作,c#,asp.net,asp.net-mvc,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,此上载图像文件的代码在本地主机上构建时运行良好。但在部署时,它不工作,也不会给出任何错误 我尝试将imagePath从/Content/Images/Items/更改为~/Content/Images/Items/和Content/Images/Items/。仍然没有解决办法 [HttpPost] public ActionResult AddProduct(ProductDisplay productDisplay,HttpPostedFileBase upload) { bool i

此上载图像文件的代码在本地主机上构建时运行良好。但在部署时,它不工作,也不会给出任何错误

我尝试将imagePath从
/Content/Images/Items/
更改为
~/Content/Images/Items/
Content/Images/Items/
。仍然没有解决办法

[HttpPost]
public ActionResult AddProduct(ProductDisplay productDisplay,HttpPostedFileBase upload)
{
    bool isSaved = false;
    string fileName = string.Empty;
    string imagePath = string.Empty;
    try
    {
        if(upload!=null && upload.ContentLength>0)
        {
            fileName = System.IO.Path.GetFileName(upload.FileName);
            imagePath = "/Content/Images/Items/" + fileName;
            upload.SaveAs(Server.MapPath(imagePath));
        }
        else
            imagePath = "/Content/Images/Items/" + "NoImage.jpg";
        productDisplay.ImagePath = imagePath;
        ProductMangementBL balProduct = new ProductMangementBL();
        isSaved = balProduct.AddProduct(productDisplay);
    }
    catch (Exception ex)
    {
        isSaved = false;
    }
    return RedirectToAction("ProductList", new RouteValueDictionary(new { controller = "Product", action = "ProductList", Id = productDisplay.CategoryID }));
}

可能需要检查应用程序池的运行状态,以及它是否具有写入文件的权限。

需要检查的一些事项:

var mapped = Server.MapPath(imagePath);
if (File.Exists(mapped))
{
  //
}
else
{
  throw new FileNotFoundException(imagePath);
}

换句话说,图像可能不在那里。

您需要创建testProductAdd操作和相应的测试视图

然后尝试只运行这段代码而不进行任何异常处理

fileName = System.IO.Path.GetFileName(upload.FileName);
imagePath = "/Content/Images/Items/" + fileName;
upload.SaveAs(Server.MapPath(imagePath));

通过这种方式,您可以找到问题所在。

在异常处理中添加一个日志函数,因为现在您只需设置一个布尔值,在退出该函数之前不再使用它,因此会丢失异常中的信息

将异常写入文本文件,它将为您提供错误的原因并指导您找到解决方案

[HttpPost]
public ActionResult AddProduct(ProductDisplay productDisplay,HttpPostedFileBase upload)
{
    bool isSaved = false;
    string fileName = string.Empty;
    string imagePath = string.Empty;
    try
    {
        if(upload!=null && upload.ContentLength>0)
        {
            fileName = System.IO.Path.GetFileName(upload.FileName);
            imagePath = "/Content/Images/Items/" + fileName;
            upload.SaveAs(Server.MapPath(imagePath));
        }
        else
            imagePath = "/Content/Images/Items/" + "NoImage.jpg";
        productDisplay.ImagePath = imagePath;
        ProductMangementBL balProduct = new ProductMangementBL();
        isSaved = balProduct.AddProduct(productDisplay);
    }
    catch (Exception ex)
    {
        isSaved = false;
    }
    return RedirectToAction("ProductList", new RouteValueDictionary(new { controller = "Product", action = "ProductList", Id = productDisplay.CategoryID }));
}
您可以使用类似于
StreamWriter
的简单方法将异常写入文本文件

例如:

try
{
    if(upload!=null && upload.ContentLength>0)
    {
        fileName = System.IO.Path.GetFileName(upload.FileName);
        imagePath = "/Content/Images/Items/" + fileName;
        upload.SaveAs(Server.MapPath(imagePath));
    }
    else
        imagePath = "/Content/Images/Items/" + "NoImage.jpg";
    productDisplay.ImagePath = imagePath;
    ProductMangementBL balProduct = new ProductMangementBL();
    isSaved = balProduct.AddProduct(productDisplay);
}
catch (Exception ex)
{
    using (StreamWriter writer = new StreamWriter("C:\\log.txt", true))
    {
        writer.WriteLine(ex);
    }
}
这会将错误消息和堆栈跟踪写入C:驱动器中的文本文档“log.txt”。

  • 将“网络服务”帐户分配到目标文件夹
  • 然后授予“网络服务”完全权限(不安全)
  • 然后将文件移动到另一个文件夹(可选选项)
更安全的选择:

  • 将文件上载到其他服务器
  • 用防病毒软件扫描文件
  • 然后使用第三方软件(如MoveIt)将文件拉入生产服务器

可能是您的服务器上不存在相应的文件夹

保存文件之前,请考虑检查文件夹是否存在:

string directory=Server.MapPath("/Content/Images/Items/");
if(!Directory.Exists(directory))
{
    Directory.CreateDirectory(directory);
}
upload.SaveAs(Path.Combine(directory,fileName));

是否引发异常?我只看到您在catch块中设置了一个布尔值,但没有看到任何日志记录,因此抛出的任何异常都会被吞没,而告诉出错原因的有用信息也会丢失。在不知道例外情况的情况下,我怀疑您是否能找到解决方案。它不应该是
\并且在开始时是@
签名吗?问题在于应用程序池权限。感谢大家把这些想法放在这里。解释一下为什么这会帮助他发现问题。1)你不需要尝试生产代码。2) 这样,您只需测试文件上载和文件保存。没有其他逻辑会影响这段代码,这会让你感到困惑。^^^这并不能解释为什么它会帮助解决他的问题。tbh真正需要的是异常处理程序中的日志函数,因为现在异常正在被吞没。不管你喜欢与否,他已经在试验生产代码了,因为他不知道问题出在哪里,也不能在本地复制。你可以在生产服务器上创建测试视图,并尽可能多地进行试验。它不会影响实际的代码。关键是。您可以使用生产环境。