【问题标题】:MVC3 Application cannot save files to directory no errors returnedMVC3 应用程序无法将文件保存到目录没有返回错误
【发布时间】:2012-01-10 14:34:42
【问题描述】:

我会先做一个简短的总结,然后尽可能添加细节。

我有一个 MVC3 应用程序在两台服务器上运行。我会打电话给他们box1box2。两台服务器都运行相同的操作系统、IIS 版本、从 subversion 加载的相同应用程序。 box1 绝对完美无瑕。它可以执行上传到 web.config 中指定的路径。但是,当我尝试使用 box2 上传时,目录已创建,因此我的 Path.Combine 语句工作正常。但从未创建实际上传的文件。

我没有收到任何错误消息,根本没有上传。我认为这是由于路径的应用程序设置。我想也许box2(在不同的域中)没有权利。这不是真的有两个原因

  1. 当我将路径设置为无法访问的内容时,我确实收到拒绝访问错误。
  2. 我什至尝试直接在它的硬盘上保存到box2。仍然只创建文件夹。

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase uploadfile, string id)
    {
    
            var dr405 = new DR405Service().GetDR405ById(new DR405DBContext(), DR405Profile.CurrentUser.TangiblePropertyId);
            var saveLocation = Path.Combine(DR405Service.SavePath + DR405Profile.CurrentUser.TangiblePropertyId);
            System.IO.Directory.CreateDirectory(saveLocation);
            if ((int)uploadfile.ContentLength / 1024 <= 15000)
            {
    
                uploadfile.SaveAs(Path.Combine(saveLocation, Path.GetFileName(uploadfile.FileName)));
                var file = new dr405files { TangiblePropertyId = DR405Profile.CurrentUser.TangiblePropertyId, FileName = uploadfile.FileName, UploadDate = DateTime.Now };
                //dr405.dr405files.Add(file); 
                //c.dr405s.Add(dr405);
    
                db.Entry(file).State = file.FileId == 0 ? EntityState.Added : EntityState.Modified;
                //db.Entry(dr405).State = EntityState.Modified;
    
                new DR405Service().Save(db);
                ViewData["UploadStatus"] = String.Format("File name: {0}, {1}Kb Uploaded Successfully.", uploadfile.FileName, (int)uploadfile.ContentLength / 1024);
            }
            else
            {
                ViewData["UploadStatus"] = String.Format("File exceeds 15MB upload limit.  Please reduce size and try again.", uploadfile.FileName);
            }
    
    
        return View();
    }
    

这是我正在克服的一些障碍

  • box2 是生产服务器,所以无法安装远程调试器
  • box2 由另一个组托管,我必须告诉他们如何配置它。他们不会或没有能力弄清楚如何解决这个问题。

更新

虽然不是最优雅的代码,但这确实可以正常工作。

更新 #2

我修改了我的操作以重定向到谷歌,因为我实际上还不能在远程服务器上运行调试器。我发现在box1 上发生了重定向。在box2 上,不会发生重定向。这让我非常确定控制器操作甚至不会在box2 上被调用。这怎么可能?

[HttpPost]
public ActionResult Upload(HttpPostedFileBase uploadfile)
{
    Response.Redirect("http://www.google.com");
        var dr405 = new DR405Service().GetDR405ById(new DR405DBContext(), DR405Profile.CurrentUser.TangiblePropertyId);
        var saveLocation = Path.Combine(DR405Service.SavePath  + DR405Profile.CurrentUser.TangiblePropertyId);
        System.IO.Directory.CreateDirectory(saveLocation);
        if ((int)uploadfile.ContentLength / 1024 <= 15000)

更新 #3

无论我使用哪个版本的 Path.Combine,它都不会改变任何东西。请重新阅读问题。 Action 似乎甚至没有被 box2 调用。

【问题讨论】:

  • 即使不保存文件也会写入成功信息吗?
  • 不,它只是刷新页面。什么都没有写。
  • 仅供参考。你的Path.Combine 有缺陷。
  • @DanielA.White 你能详细说明它有什么缺陷吗?
  • 我认为 Daniel 是对的——你不应该在其中使用逗号而不是 + 吗?

标签: c# asp.net-mvc-3 iis-6


【解决方案1】:

改变

var saveLocation = Path.Combine(DR405Service.SavePath + DR405Profile.CurrentUser.TangiblePropertyId);

var saveLocation = Path.Combine(DR405Service.SavePath, DR405Profile.CurrentUser.TangiblePropertyId);

【讨论】:

  • 虽然您在技术上可能是正确的。该应用程序在 box1 上正常工作,无需进行此更改。这会影响运行我的应用程序的所有服务器。
  • 仍然,代码是错误的。以这种方式调用Path.Combine() 没有任何作用。
  • 不,你错了。虽然它可能不是最优雅的代码。
  • @DougChamberlain:不,你实际上,真的,错了。 Documentation
  • 你的方法是错误的,因为你所做的只是用一个参数调用 Path.Combine。它违背了使用 Path.Combine 的目的,即自动管理路径分隔符。即,如果您使用“c:\abc”和“file.txt”调用 Path.Combine,它将返回“c:\abc\file.txt”。您的方法将返回“c:\abcfile.txt”。我同意这可能不是这里的根本问题。
猜你喜欢
  • 2022-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多