【问题标题】:My controller couldnt save physical file path in production我的控制器无法在生产中保存物理文件路径
【发布时间】:2021-06-18 19:33:13
【问题描述】:

我试图通过我的控制器将文件更新到物理上。但是我的控制器完美地保存在本地(实际上是 Visual Studio 运行),但是当我尝试在我的服务器上发布和设置并使用 www.mywebsite.com 时,控制器无法保存到路径而不出现错误。

我的场景是这样的:

  1. 在客户端 onReady js 方法中,我从我的数据库中获取物理根路径(这对双方都有效,我看调试控制台没问题)。我的物理路径是这样的:\\192.168.1.1\MYFILE-1.1

  2. 我通过模型使用 POST 方法文件和根路径传递值

  3. 在控制器端,我为文件名创建时间戳,并结合更多路径文件。像这样我的控制器:

    [HttpPost]
    public JsonResult SavePhysicalPath(FileModel model)
    {
         //create timestamp of file name
          string filesMimeType = MimeTypesMap.GetMimeType(model.FormFile.FileName);
          string filesExtType = MimeTypesMap.GetExtension(filesMimeType);
          string fnTimeStamp = DateTime.Now.ToString("ddMMyyyy_HHmmssffff");
          string edittedFileName = fnTimeStamp + "." + filesExtType;
          string edittedFmAndSubPath = "MyDocs\\OtherFiles\\" +edittedFileName ;
          var savingRootPath = "";
            savingRootPath =model.FileFolderPath; // FileFolderPath is string get from view "\\192.168.1.1\MYFILE-1.1"
    
           try
           {
                string SavePath = Path.Combine(Directory.GetCurrentDirectory(), savingRootPath, edittedFmAndSubPath );
    
                using (var stream = new FileStream(SavePath, FileMode.Create))
                {
                    model.FormFile.CopyTo(stream);
                }
    
                stringOutput = "OK";
                return Json(stringOutput);
           }
           catch (Exception ex)
           {
                 stringOutput = "ERR";
                 return Json(stringOutput);
                 throw;
           }
    }
    

【问题讨论】:

  • 首先你需要记录

标签: c# asp.net-core model-view-controller file-io controller


【解决方案1】:

如果没有目录,则创建一个。

bool exists = System.IO.Directory.Exists(SavePath);

if (!exists)
   System.IO.Directory.CreateDirectory(SavePath);

或者您可以在服务器上手动创建一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多