0

other data the from send form.submit() I first read the img tags value then converted it to a file , then added it to the form But its value in the controller is null

        var imgs = document.getElementsByName('images');
        var imagefiles = [];
        var data = new FormData();

        for (var i = 0; i < imgs.length; i++) {
            var image = dataURLtoFile(imgs[i].src, FileName());
            imagefiles.push(image);               
        }
        data.append('files', imagefiles);          
        $("#Form").submit();



      public ActionResult Create(ProductsModel model, HttpPostedFileBase[] files )       
      {}
6
  • You need to use AJAX to send the FormData object. Right now you're just submitting the form normally and the file content you create is being ignored. That said, if you are able to submit the form normally (and don't need to remain on the same page after submission) then you simply need to set your HTML up correctly, there's no need for JS/AJAX in that case Commented Jun 30, 2022 at 13:37
  • I also used ajax to send the FormData , but the files are null Commented Jul 2, 2022 at 0:48
  • Here's how you need to structure your code: stackoverflow.com/a/37762290/519413 Commented Jul 2, 2022 at 13:32
  • Thanks for the reply.The point is that in this example, the file is also received from the input tag But I receive the file from the img src tag . the reason is that I want to save the images after cropping Commented Jul 2, 2022 at 19:09
  • Then convert the dataUrl to a blob and append that to your FormData object: stackoverflow.com/a/30407840/519413 Commented Jul 2, 2022 at 19:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.