c# - 我的控制器无法在生产中保存物理文件路径
问题描述
我试图通过我的控制器将文件更新到物理上。但是我的控制器完美地保存在本地(实际上是 Visual Studio 运行),但是当我尝试在我的服务器上发布和设置并使用时www.mywebsite.com
,控制器无法保存到路径而不出现错误。
我的场景是这样的:
在客户端 onReady js 方法中,我从我的数据库中获取物理根路径(这对双方都有效,我看调试控制台没问题)。我的物理路径是这样的:
\\192.168.1.1\MYFILE-1.1
我通过模型使用 POST 方法文件和根路径传递值
在控制器端,我为文件名创建时间戳,并结合更多路径文件。像这样我的控制器:
[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; } }
解决方案
如果没有目录,则创建一个。
bool exists = System.IO.Directory.Exists(SavePath);
if (!exists)
System.IO.Directory.CreateDirectory(SavePath);
或者您可以在服务器上手动创建一个。
推荐阅读
- python - 如何修复'用户输入(“用户:”)语法错误'
- java - ARCore 是否有像 ARKit 那样的会话委托方法?
- sql-server-2016 - 从 spark sql(Databricks) 到 Sql Server 数据仓库的双重数据类型的替代方案是什么
- swift - 斯威夫特:一些类没有取消初始化
- html - 固定顶部导航栏隐藏其下的内容
- node.js - 如何在没有 id 的 ref 对象中查找文档?
- unity3d - 麦克风、增益值和频谱值不使用 Unity 同步
- c# - 连接两个序列,其元素交错
- c - 发送文件大小,然后发送文件本身
- powershell - 不要在 PowerShell 脚本结果中显示取消