Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 上传后返回文件的数据_C#_Asp.net_Json - Fatal编程技术网

C# 上传后返回文件的数据

C# 上传后返回文件的数据,c#,asp.net,json,C#,Asp.net,Json,我参考了一些堆栈溢出的答案,并设法创建了一个ASP.NET C#应用程序,允许用户上传文件(.txt)。当我运行应用程序时,web浏览器中会打开一个页面,显示“选择文件”和“确定”。选择一个文件并输入“Ok”后,该文件将上载到项目目录中的“uploads”文件夹 如何编辑代码,使.txt文件中的数据在单击“确定”后也以JSON格式显示在web浏览器页面上,而不是仅上载到“uploads”文件夹 我知道要读取文件,代码应该是: string data = File.ReadAllText(path

我参考了一些堆栈溢出的答案,并设法创建了一个ASP.NET C#应用程序,允许用户上传文件(.txt)。当我运行应用程序时,web浏览器中会打开一个页面,显示“选择文件”和“确定”。选择一个文件并输入“Ok”后,该文件将上载到项目目录中的“uploads”文件夹

如何编辑代码,使.txt文件中的数据在单击“确定”后也以JSON格式显示在web浏览器页面上,而不是仅上载到“uploads”文件夹

我知道要读取文件,代码应该是:

string data = File.ReadAllText(path);
return data;
然而,我不确定如何把这些代码,使程序的工作要求

以下是我迄今为止所做的工作:

Index.cshtml

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
<input type="file" name="file" />
<input type="submit" value="OK" />
}

嗯,这有点尴尬,但你可以做到

<div>@ViewBag.JSON</div>

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
<input type="file" name="file" />
<input type="submit" value="OK" />
}
更新由于您不想返回任何html,请按如下方式更改代码:

  public ActionResult Index()
  {
        return View();
  }

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0)
        {
            // extract only the filename
            var fileName = Path.GetFileName(file.FileName);
            // store the file inside ~/App_Data/uploads folder
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
            Response.Write(System.IO.File.ReadAllText(path));
            return null;
        }

        // redirect back to the index action to show the form once again
        return RedirectToAction("Index");
    }

嗨,谢谢你帮助我。我在
TempData[“JSON”]
错误下方得到一条红线,指出:
无法从对象转换为字符串
我想再问一个问题。这是可行的,但在数据的末尾,我看到了“选择文件”和“确定”按钮。我不想那样。我应该返回什么呢?另外,数据不是以JSON格式显示的,当我通过“查看页面源”查看数据时,它仍然有标记。有没有办法以完整的json格式显示它?它的工作原理与我所希望的一样!非常感谢!:)
  public ActionResult Index()
  {
    if(TempData.ContainsKey("JSON") && !string.IsNullOrEmpty((string)TempData["JSON"]))
    {
       ViewBag.JSON = System.IO.File.ReadAllText((string)TempData["JSON"]);
    }
        return View();
  }

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0)
        {
            // extract only the filename
            var fileName = Path.GetFileName(file.FileName);
            // store the file inside ~/App_Data/uploads folder
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            TempData["JSON"] = path;
            file.SaveAs(path);
        }

        // redirect back to the index action to show the form once again
        return RedirectToAction("Index");
    }
  public ActionResult Index()
  {
        return View();
  }

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file)
    {
        // Verify that the user selected a file
        if (file != null && file.ContentLength > 0)
        {
            // extract only the filename
            var fileName = Path.GetFileName(file.FileName);
            // store the file inside ~/App_Data/uploads folder
            var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
            file.SaveAs(path);
            Response.Write(System.IO.File.ReadAllText(path));
            return null;
        }

        // redirect back to the index action to show the form once again
        return RedirectToAction("Index");
    }