How can retrieve string formData js in c#

后端 未结 3 469
南笙
南笙 2021-01-02 17:58

I have to retrieve the value of \"idPerson\" in my web api in .net. I already retrieve the file \"UploadedImage\". But I can\'t retrieve the value of \"idPerson\".

S

相关标签:
3条回答
  • 2021-01-02 18:35

    Request.Form["KEY"] worked in my MVC controller.

    0 讨论(0)
  • 2021-01-02 18:38

    Html:

    <input id="image-file" type="file" onchange="SavePhoto(this)"/>
    

    JavaScript:

    <script type="text/javascript">
    
    function SavePhoto()
    {
         var photo = document.getElementById("image-file").files[0];  // file from input
         var req = new XMLHttpRequest();
         var formData = new FormData();
    
        formData.append("photo", photo );
        req.open("POST", '/upload/image2/');
        req.send(formData);       
    }
    </script>
    

    C# Controller:

        [HttpPost]
        public void Image2()
        {
            HttpPostedFileBase img2 = Request.Files["photo"];
            string path = @"D:\Server\Image\Newred\" + img2.FileName;
            img2.SaveAs(path);
        }
    
    0 讨论(0)
  • 2021-01-02 18:52

    Assuming your are sending typical Ajax POST request, you can retrieve each field from HttpContext.Current.Request.Form collection.

    Just find your key in collection like HttpContext.Current.Request.Form["KEY"]

    TBH it is hard to say how to retrieve any value when you did not provide the way of sending data.

    0 讨论(0)
提交回复
热议问题