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 )       
      {}
fafa
  • 1
  • 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 – Rory McCrossan Jun 30 '22 at 13:37
  • I also used ajax to send the FormData , but the files are null – fafa Jul 02 '22 at 00:48
  • Here's how you need to structure your code: https://stackoverflow.com/a/37762290/519413 – Rory McCrossan Jul 02 '22 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 – fafa Jul 02 '22 at 19:09
  • Then convert the dataUrl to a blob and append that to your FormData object: https://stackoverflow.com/a/30407840/519413 – Rory McCrossan Jul 02 '22 at 19:20
  • Thank you for the advice It was fixed after converting to blob – fafa Jul 03 '22 at 17:00

0 Answers0