0

I am uploading images to the server using jQuery/Ajax Like this :

JS:

var formdata = new FormData();
for (i = 0; i < fileInput.files.length; i++) {
      //Appending each file to FormData object
      formdata.append(fileInput.files[i].name, fileInput.files[i]);
 }

 //This is where I want to send additional data 
 formdata.append('id', $('#txtTest').val());

ASP MVC Controller :

    public JsonResult FileUpload()
    {

       *********   //How Can get id here ?  ********** 

        for (int i = 0; i < Request.Files.Count; i++)
        {
            HttpPostedFileBase file = Request.Files[i];
            int fileSize = file.ContentLength;
            string fileName = new DateTime().GetTimestamp(DateTime.Now) + "," + file.FileName;
            System.IO.Stream fileContent = file.InputStream;
            file.SaveAs(Server.MapPath("~/UploadImages") + fileName); 
        }
        return Json(new { message = Request.Files.Count});
    }

How can get the additional property in Controller?

mohammad
  • 1,162
  • 2
  • 12
  • 25
  • Just change the method to `public JsonResult FileUpload(int id)` (or `string id` or whatever depending on the data type) –  Feb 14 '17 at 12:09
  • And it will be easier is you use `formdata.append('files', fileInput.files[i]);` and `public JsonResult FileUpload(int id, IEnumerable –  Feb 14 '17 at 12:10
  • I used to it, but it doesn't work. – mohammad Feb 14 '17 at 12:15
  • Of course it works! –  Feb 14 '17 at 20:35

0 Answers0