【问题标题】:Return data of file after it has been uploaded上传文件后返回文件数据
【发布时间】:2018-01-10 01:03:30
【问题描述】:

我参考了一些 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");
    }
} 

【问题讨论】:

    标签: c# asp.net json


    【解决方案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");
        }
    

    【讨论】:

    • 您好,感谢您对我的帮助。我在TempData["JSON"] 下得到一条红线错误说明:cannot convert from object to string
    • 我想再问一个问题。这可行,但在数据末尾,我看到“选择文件”和“确定”按钮。我不要那个。我应该返回什么?
    • 另外,数据不是以 JSON 格式显示的,当我通过“查看页面源”查看它时,它仍然具有 标签。有没有办法以完整的json格式显示?
    • 它就像我想要的那样工作!太感谢了 ! :)
    猜你喜欢
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2020-04-03
    相关资源
    最近更新 更多