0

不知道标题有多清晰。我有文件上传脚本。输入字段是用javascript动态创建的,名称为testdata。为用户决定上传的每个文件创建一个输入字段。到目前为止,我的模型只是获得了该字段的值。

 public string testdata { get; set; }

稍后在我的控制器中,我获取值并将其写入文件。问题是当上传多个文件时,控制器从所有输入中收集值并将它们一起写在每一行。我想要的是每个输入(每次上传)的值都有一个单独的行。控制器只做我要求它做的事情。我不知道如何访问输入字段的 id。我知道我需要改变我的模型,但不确定如何。任何帮助将不胜感激。谢谢!

控制器,以防它不是很清楚:

        [HttpPost]
    public ActionResult Index(UploadsModel model, IEnumerable<HttpPostedFileBase> files)
    {
        string uploadDir = Server.MapPath("~/App_Data/uploads");
        foreach (string fileKey in Request.Files)
        {
            HttpPostedFileBase file = Request.Files[fileKey];
            StreamWriter tw = System.IO.File.AppendText(Path.Combine(uploadDir, "post_data.txt"));
            tw.WriteLine(System.DateTime.Now + " " + file.FileName + " " + model.testdata);
            tw.Close();
            file.SaveAs(Path.Combine(uploadDir, file.FileName));
        }

        //could go somewhere else...
        return RedirectToAction("Index");
    }

编辑:找到解决方法。将为每个常规字段动态创建一个隐藏字段。将第一个值传递给它,将在我的模型中创建新属性,上传和保存时,我将比较文件名和隐藏字段值。

4

0 回答 0