首页 > 解决方案 > 我的控制器无法在生产中保存物理文件路径

问题描述

我试图通过我的控制器将文件更新到物理上。但是我的控制器完美地保存在本地(实际上是 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-coremodel-view-controllerfile-iocontroller

解决方案


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

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

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

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


推荐阅读