c# - “Could not find a part of the path” Azure WebApp 中的文件读取操作错误

标签 c# .net asp.net-mvc azure azure-web-app-service

我有 MVC 应用程序,它具有浏览按钮,我选择文件的任何位置并使用路径读取文件内容,然后处理内容。

在本地工作正常,但当在 azure 上作为 Web 应用程序发布时,显然无法获取文件系统路径,但如何处理这个问题?

Could not find file 'D:\Windows\system32\mydata.json'.

Index.cshtml

 <label>File Path</label>
    <table>
      <tr>
        <td>@Html.TextBoxFor(m => m.filePath, new { type = "file", @class = "input-file" }) )</td>
        <td>&nbsp;&nbsp;</td>

      </tr>
    </table>

HomeController.cs

private static void Test(string filepath)
        {
            string data = System.IO.File.ReadAllText(filepath);
            JArray array = JArray.Parse(data);

最佳答案

在 Azure 上,进程当前工作目录为 D:\Windows\system32\,尝试 var WholePath = Path.Combine(Server.MapPath("~/"), filepath); 定位 Web 根目录下的文件。

更新

HttpPostedFileBase 字段添加到您的模型中。在您的 View 中,更改为 m => m.File

public class FileModel 

{
    public HttpPostedFileBase File { get; set; }
}

在 Controller 中

public ActionResult FileUpload(FileModel fileModel)
{
    if (ModelState.IsValid)
    {
         StreamReader s = new StreamReader(fileModel.File.InputStream);
         JArray array = JArray.Parse(s.ReadToEnd());
         ...
     }
     return View();
}

关于c# - “Could not find a part of the path” Azure WebApp 中的文件读取操作错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53108415/

相关文章:

c# - 根据达到 1 所需的 Collat​​z 猜想循环次数找到一个数字

.net - 此 xaml 代码中的符号 {} 是什么意思?

c# - 如何修复此 ArrayIndex 错误?

c# - 本地 IIS 无法连接本地数据库

.net - 无法进行调试和数据库访问时处理错误的方法

.net - 将命令分配给 EventSetter

asp.net-mvc - 如何在局部 View 中使用ViewModels

javascript - 将导航栏菜单类设置为事件状态,直到单击另一个菜单或直到用户停留在某个 Controller 中?

c# - 在 Razor-4 View 中访问模型属性

c# - 雷达图颜色内圈