3

我正在使用自定义引导程序,链接:http: //jasny.github.io/bootstrap/javascript.html#fileupload我有一个上传文件的按钮。这个文件是一个 JSON,我想在我的控制器上读取它。我怎样才能做到这一点?带有文件参数的方法并使用 Json Parser 读取此文件。

4

2 回答 2

3

只需创建您的表单并创建您的操作,如下所示:

using System.Web.Script.Serialization;

[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    if (file.ContentLength > 0)
    {
        // get contents to string
        string str = (new StreamReader(file.InputStream)).ReadToEnd();

        // deserializes string into object
        JavaScriptSerializer jss = new JavaScriptSerializer();
        var d = jss.Deserialize<dynamic>(str);

        // once it's an object, you can use do with it whatever you want
    }
}
于 2013-08-20T16:45:05.903 回答
2

查看 JSON 解析器。如果你去这里 ( http://theburningmonk.com/benchmarks/ ) 并向下滚动到 JSON 序列化器,你会看到一个流行的序列化器列表和它们的性能。

根据您对 JSON 的需求,您可以反序列化为动态的,并且非常容易地使用.运算符访问对象的成员。您的另一个选择是使用http://json2csharp.com/生成与 JSON 布局匹配的类文件,并允许您反序列化为精确对象。

至于上传部分,我建议你看看这里:文件上传 ASP.NET MVC 3.0

于 2013-08-20T16:39:06.743 回答