我有 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> </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/