Azure WebApp中的文件读取操作错误“找不到路径的一部分" [英] “Could not find a part of the path” Error For File Read Operation in Azure WebApp

查看:112
本文介绍了Azure WebApp中的文件读取操作错误“找不到路径的一部分"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有MVC application,其中有一个浏览按钮,我要选择文件的任何位置,然后使用路径读取文件内容,然后处理内容.

I have MVC application which is having browse button I'm selecting file any location and reading file content using path and then process the content.

在本地工作正常,但是当作为Web应用程序在azure上发布时,很明显它无法获取文件系统路径,但是如何处理呢?

Works fine in local but when published on azure as web app obvious it was not able to get the file system path but how to handle this?

找不到文件'D:\ Windows \ system32 \ mydata.json'.

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根目录下找到文件.

On Azure the process current working directory is D:\Windows\system32\, try var wholePath = Path.Combine(Server.MapPath("~/"), filepath); to locate files under web root.

更新

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

Add HttpPostedFileBase field to your Model. In your View, change to m => m.File.

public class FileModel 

{
    public HttpPostedFileBase File { get; set; }
}

在控制器中

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

这篇关于Azure WebApp中的文件读取操作错误“找不到路径的一部分"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆