上传文件后返回文件数据

时间:2018-01-10 01:03:31

标签: c# asp.net json

我已经提到了一些Stack Overflow的答案,并设法创建一个允许用户上传文件(.txt)的ASP.NET C#应用程序。当我运行应用程序时,会在Web浏览器中打开一个页面,其中显示“选择文件”和“确定”。选择文件并输入“Ok”后,文件将上传到项目目录中的“uploads”文件夹。

如何编辑代码而不是刚刚上传到“uploads”文件夹的文件,单击“确定”后,.txt文件中的数据会显示在JSON的Web浏览器页面上?

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

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" />
}

HomeController.cs

public class HomeController : Controller
{
    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);
        }
        // redirect back to the index action to show the form once again
        return RedirectToAction("Index");
    }
} 

1 个答案:

答案 0 :(得分:1)

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

<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" />
}

然后在您的控制器中

  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");
    }

更新,因为您不想返回任何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");
    }