1

I want to send data from the form (file from input and string from input) using ajax to ASP.NET Function. If I send only files i use:

function readURL() {
 var input = document.getElementById("fileUpload");
 var files = input.files;
 var formData = new FormData();
 var test = "some text";
 for (var i = 0; i != files.length; i++) {
   formData.append("files", files[i]);
  }
 $.ajax({
  type: "POST",
  url: '/Apartment/UploadFiles',
  data: {files: formData },
  processData: false,
  contentType: false,
 });
}

If I want to send an only string, I use:

function readURL() {
 var input = document.getElementById("fileUpload");
 var files = input.files;
 var formData = new FormData();
 var test = "some text";
 for (var i = 0; i != files.length; i++) {
   formData.append("files", files[i]);
  }
 $.ajax({
  type: "POST",
  url: '/Apartment/UploadFiles',
  data: { test: test},
  dataType: "json",
 });
}

It is possible to send string and FormData using one Ajax? I try something like this:

$.ajax({
 type: "POST",
 url: '/Apartment/UploadFiles',
 data: {files: formData, test: test },
 processData: false,
 contentType: false,
});

but now the string parameter is not sending (is null).

Additional - My Controller code

public async Task<IActionResult> UploadFiles(string test, IList<IFormFile> files)
        {
            ...
        }
5
  • 1
    is test an actual variable with a string value or did you miss the quotes ? You can add values to formData Object with formData.append(name, value); Commented Nov 4, 2020 at 13:51
  • in code for test it i create string variable in function where is located AJAX var test = "some txt"; Commented Nov 4, 2020 at 13:52
  • 1
    try to append your string to the formdata object as mentioned above Commented Nov 4, 2020 at 13:54
  • i try use formData.append(test, value) but now it doesn't work (file is not sending). I think that NAME parameter (formData.append(NAME, value) must be this same name who i using in ASP.Function public async Task<IActionResult> UploadFiles(IList<IFormFile> NAME). In other case this doesn't work Commented Nov 4, 2020 at 14:05
  • 1
    Appending a value to your formData Object will not work in that case . I am not familiar with ASP, that being said, i guess you would have to create a new dto that includes the string and your IFormFile and use that dto in your UploadFiles function as a parameter. Then you can access the string and the file withing that dto in your function Commented Nov 4, 2020 at 14:20

1 Answer 1

2

It's not possible to combine multiple Content Types when sending a FormData object.

Append the string to the formData instance and set the name to test.

function readURL() {
  var input = document.getElementById("fileUpload");
  var files = input.files;
  var formData = new FormData();
  var test = "some text";

  for (var i = 0; i != files.length; i++) {
    formData.append("files", files[i]);
  }

  formData.append("test", test);

  $.ajax({
    type: "POST",
    url: '/Apartment/UploadFiles',
    data: formData,
    dataType: "json",
    processData: false,
    contentType: false,
  });
} 
Sign up to request clarification or add additional context in comments.

1 Comment

Omg you are soo clever, I would never thought of that! Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.