如何将 Id 和文件发送到同一个控制器?

How to send Id and file to same controller?

"i want to send id and file data to same action uploadFile(int id, httpPostFileBase upFile) ?"

我尝试在提交期间通过 ajax 发送患者 ID,并在输入标签中使用名称属性进行归档。

@using (Html.BeginForm("uploadFile", "patientsProfile", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {

        <input type="file" name="upFile" />
        <br />
        <input type="submit" name="submit" value="Upload!" />
    }
var id = url.substring(url.lastIndexOf('/') + 1);

$("form").submit(function (e) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Action("uploadFile","patientsProfile")",
                    data: {
                        Id: id
                    },
                    success: function (res) {
                        alert("id :" + id);
                    }
                })
            })
[HttpPost]
        public ActionResult uploadFile( HttpPostedFileBase upFile , int Id)
        {
            Tests tests = new Tests();

            tests.patients_Id = Id;

            string fileName = upFile.FileName;

            string UniquefileName = Guid.NewGuid() + Path.GetFileName(upFile.FileName);

            string filePath = Server.MapPath("~/Uploaded/");


            string actualPath = Path.Combine(filePath + UniquefileName);
            upFile.SaveAs(actualPath);

            tests.Name = fileName;
            tests.urlName = actualPath;
            db.Tests.Add(tests);
            db.SaveChanges();

            return RedirectToAction("index");
        }

httpPostFileBase upFile 为 null 但 id 正确取值

首先我认为下面的代码会导致问题

var id = url.substring(url.lastIndexOf('/') + 1);

原因

  • 如果在id只有一个字符的情况下用它来获取id还可以,但是当字符数增加的时候就会获取错误的值。例如 url www.yourdomain.com/controller/action/1 它可以正常工作,但是 www.yourdomain.com/controller/action/12 将 return 与第一个结果相同。所以你可能想在那里做一些数学运算,比如 (stringLength - lastIndexOf / )

我建议您使用 ViewBag 获取该 Id,因为它是从 GET 方法传递的,然后将其作为参数传递给表单,如下所示

@using (Html.BeginForm("uploadFile", "Home", new{Id = ViewBag.Id}, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="upFile" id="upFile" />
    <br />
    <input type="submit" name="submit" value="Upload!" />
}

记得在get请求中包含视图包作为

public ActionResult ActionName(int Id)
{
    ViewBag.Id = Id;
    return View();
}

如果您坚持使用 javascript 然后尝试将文件作为数据包含在 answer

您必须将 upFile 传递给您要发送的数据。

data: {
    Id: id,
    upFile: //the file which you are sending
},
       @using (Html.BeginForm("uploadFile", "Home", new{Id = ViewBag.Id}, 
       FormMethod.Post, new { enctype = "multipart/form-data" }))
       {
       <input type="file" name="upFile" id="upFile" />
       <input type="hidden" name="hdfid" id="hdfid" value ="" />
       <br />
       <input type="submit" name="submit" value="Upload!" />
       }


       var id = url.substring(url.lastIndexOf('/') + 1);

         $("#hdfid").val(id);
            $("form").submit(function (e) {
            $.ajax({
                type: "POST",
                url: "@Url.Action("uploadFile","patientsProfile")",

                success: function (res) {
                    alert("id :" + id);
                }
            })
        })







        public ActionResult uploadFile( formcollection form, HttpPostedFileBase 
        upFile )
        {
            Tests tests = new Tests();
            string id = form["id"];//or provide index
            tests.patients_Id = Id;

            string fileName = upFile.FileName;

            string UniquefileName = Guid.NewGuid() + 
            Path.GetFileName(upFile.FileName);

            string filePath = Server.MapPath("~/Uploaded/");


            string actualPath = Path.Combine(filePath + UniquefileName);
            upFile.SaveAs(actualPath);

            tests.Name = fileName;
            tests.urlName = actualPath;
            db.Tests.Add(tests);
            db.SaveChanges();

            return RedirectToAction("index");
        }