首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP.NET MVC文件上载在本地构建上运行,在部署后不运行

问ASP.NET MVC文件上载在本地构建上运行,在部署后不运行
EN

Stack Overflow用户
提问于 2015-08-05 20:53:46
回答 6查看 4.2K关注 0票数 6

这个上传图像文件的代码在构建于localhost上时运行良好。但在部署时,它不工作,也不会给出任何错误。

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

代码语言:javascript
复制
[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 }));
}
EN

回答 6

Stack Overflow用户

发布于 2015-08-05 20:59:55

以下是需要检查的内容:

代码语言:javascript
复制
var mapped = Server.MapPath(imagePath);
if (File.Exists(mapped))
{
  //
}
else
{
  throw new FileNotFoundException(imagePath);
}

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

票数 3
EN

Stack Overflow用户

发布于 2015-08-05 20:59:04

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

票数 2
EN

Stack Overflow用户

发布于 2015-08-05 21:12:42

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

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

代码语言:javascript
复制
fileName = System.IO.Path.GetFileName(upload.FileName);
imagePath = "/Content/Images/Items/" + fileName;
upload.SaveAs(Server.MapPath(imagePath));

这样您就可以找出问题所在。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31833138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档