Remove selected file(s) by clicking the remove link from the input file multiple and submit to Controller's...












1














How do I post the file(s) to the parameter in my Controller?
I have a breakpoint for my Action in my Controller and I want to see if the files (after removed some unwanted files) can be post through my Controller.



Right now, even if I hit upload button, the breakpoint will give me a null value instead of the name of the files.



After clicking the submit button, the files displayed in the Console (F12) is alright, but for the breakpoint in my Controller is showing null.



What should I do to post the files to my Controller?



My Controller:



enter image description here



The client side scripts :






var formData = new FormData();
var fileList = ;
var fileInput = document.querySelector('input[type="file"]');

fileInput.addEventListener('change', setList);
document.querySelector('form').addEventListener('submit', sendModifiesList);

function setList() {
//Convert the FileList Object to an Array of File Objects
fileList = Array.from(fileInput.files);
outputList();
}


function sendModifiesList(e) {
e.preventDefault();
fileList.forEach(function(file) {
formData.append('fileList', file);
});
console.log("These files will be posted: ", formData.getAll("fileList"));
/*************** EDIT *************************/
// Get the url from the form's action attribute
let url = document.forms[0].action;
let request = new XMLHttpRequest();
// Create a POST request
request.open("POST", url);
// Set up an onload handler to report status
request.onload = function() {
if (request.status == 200) {
console.log("Uploaded!");
} else {
console.log("Error " + request.status + " occurred when trying to upload your file.");
}
};
// Send the form to the server
request.send(formData);
/************ END EDIT ***********************/
};

function outputList() {
var output = document.getElementById('fileList');
var nodes = document.createElement('ul');
fileList.forEach((file, i) => {
var node = document.createElement('li');
var filename = document.createTextNode(file.name);
var removeLink = document.createElement('a');
removeLink.href = 'javascript:void(0)';
removeLink.innerHTML = 'Remove';
removeLink.onclick = function() {
// Remove item
fileList.splice(i, 1);
outputList();
}
node.appendChild(filename);
node.appendChild(removeLink);
nodes.appendChild(node);
});
output.innerHTML = '';
output.appendChild(nodes);
}

<form asp-action="UploadAction" method="post" class="form-horizontal" enctype="multipart/form-data">
<input type="file" multiple>
<input type="submit">
</form>
<div id="fileList"></div>












share|improve this question
























  • Yes, I have added.
    – Matt
    Dec 28 '18 at 4:49
















1














How do I post the file(s) to the parameter in my Controller?
I have a breakpoint for my Action in my Controller and I want to see if the files (after removed some unwanted files) can be post through my Controller.



Right now, even if I hit upload button, the breakpoint will give me a null value instead of the name of the files.



After clicking the submit button, the files displayed in the Console (F12) is alright, but for the breakpoint in my Controller is showing null.



What should I do to post the files to my Controller?



My Controller:



enter image description here



The client side scripts :






var formData = new FormData();
var fileList = ;
var fileInput = document.querySelector('input[type="file"]');

fileInput.addEventListener('change', setList);
document.querySelector('form').addEventListener('submit', sendModifiesList);

function setList() {
//Convert the FileList Object to an Array of File Objects
fileList = Array.from(fileInput.files);
outputList();
}


function sendModifiesList(e) {
e.preventDefault();
fileList.forEach(function(file) {
formData.append('fileList', file);
});
console.log("These files will be posted: ", formData.getAll("fileList"));
/*************** EDIT *************************/
// Get the url from the form's action attribute
let url = document.forms[0].action;
let request = new XMLHttpRequest();
// Create a POST request
request.open("POST", url);
// Set up an onload handler to report status
request.onload = function() {
if (request.status == 200) {
console.log("Uploaded!");
} else {
console.log("Error " + request.status + " occurred when trying to upload your file.");
}
};
// Send the form to the server
request.send(formData);
/************ END EDIT ***********************/
};

function outputList() {
var output = document.getElementById('fileList');
var nodes = document.createElement('ul');
fileList.forEach((file, i) => {
var node = document.createElement('li');
var filename = document.createTextNode(file.name);
var removeLink = document.createElement('a');
removeLink.href = 'javascript:void(0)';
removeLink.innerHTML = 'Remove';
removeLink.onclick = function() {
// Remove item
fileList.splice(i, 1);
outputList();
}
node.appendChild(filename);
node.appendChild(removeLink);
nodes.appendChild(node);
});
output.innerHTML = '';
output.appendChild(nodes);
}

<form asp-action="UploadAction" method="post" class="form-horizontal" enctype="multipart/form-data">
<input type="file" multiple>
<input type="submit">
</form>
<div id="fileList"></div>












share|improve this question
























  • Yes, I have added.
    – Matt
    Dec 28 '18 at 4:49














1












1








1







How do I post the file(s) to the parameter in my Controller?
I have a breakpoint for my Action in my Controller and I want to see if the files (after removed some unwanted files) can be post through my Controller.



Right now, even if I hit upload button, the breakpoint will give me a null value instead of the name of the files.



After clicking the submit button, the files displayed in the Console (F12) is alright, but for the breakpoint in my Controller is showing null.



What should I do to post the files to my Controller?



My Controller:



enter image description here



The client side scripts :






var formData = new FormData();
var fileList = ;
var fileInput = document.querySelector('input[type="file"]');

fileInput.addEventListener('change', setList);
document.querySelector('form').addEventListener('submit', sendModifiesList);

function setList() {
//Convert the FileList Object to an Array of File Objects
fileList = Array.from(fileInput.files);
outputList();
}


function sendModifiesList(e) {
e.preventDefault();
fileList.forEach(function(file) {
formData.append('fileList', file);
});
console.log("These files will be posted: ", formData.getAll("fileList"));
/*************** EDIT *************************/
// Get the url from the form's action attribute
let url = document.forms[0].action;
let request = new XMLHttpRequest();
// Create a POST request
request.open("POST", url);
// Set up an onload handler to report status
request.onload = function() {
if (request.status == 200) {
console.log("Uploaded!");
} else {
console.log("Error " + request.status + " occurred when trying to upload your file.");
}
};
// Send the form to the server
request.send(formData);
/************ END EDIT ***********************/
};

function outputList() {
var output = document.getElementById('fileList');
var nodes = document.createElement('ul');
fileList.forEach((file, i) => {
var node = document.createElement('li');
var filename = document.createTextNode(file.name);
var removeLink = document.createElement('a');
removeLink.href = 'javascript:void(0)';
removeLink.innerHTML = 'Remove';
removeLink.onclick = function() {
// Remove item
fileList.splice(i, 1);
outputList();
}
node.appendChild(filename);
node.appendChild(removeLink);
nodes.appendChild(node);
});
output.innerHTML = '';
output.appendChild(nodes);
}

<form asp-action="UploadAction" method="post" class="form-horizontal" enctype="multipart/form-data">
<input type="file" multiple>
<input type="submit">
</form>
<div id="fileList"></div>












share|improve this question















How do I post the file(s) to the parameter in my Controller?
I have a breakpoint for my Action in my Controller and I want to see if the files (after removed some unwanted files) can be post through my Controller.



Right now, even if I hit upload button, the breakpoint will give me a null value instead of the name of the files.



After clicking the submit button, the files displayed in the Console (F12) is alright, but for the breakpoint in my Controller is showing null.



What should I do to post the files to my Controller?



My Controller:



enter image description here



The client side scripts :






var formData = new FormData();
var fileList = ;
var fileInput = document.querySelector('input[type="file"]');

fileInput.addEventListener('change', setList);
document.querySelector('form').addEventListener('submit', sendModifiesList);

function setList() {
//Convert the FileList Object to an Array of File Objects
fileList = Array.from(fileInput.files);
outputList();
}


function sendModifiesList(e) {
e.preventDefault();
fileList.forEach(function(file) {
formData.append('fileList', file);
});
console.log("These files will be posted: ", formData.getAll("fileList"));
/*************** EDIT *************************/
// Get the url from the form's action attribute
let url = document.forms[0].action;
let request = new XMLHttpRequest();
// Create a POST request
request.open("POST", url);
// Set up an onload handler to report status
request.onload = function() {
if (request.status == 200) {
console.log("Uploaded!");
} else {
console.log("Error " + request.status + " occurred when trying to upload your file.");
}
};
// Send the form to the server
request.send(formData);
/************ END EDIT ***********************/
};

function outputList() {
var output = document.getElementById('fileList');
var nodes = document.createElement('ul');
fileList.forEach((file, i) => {
var node = document.createElement('li');
var filename = document.createTextNode(file.name);
var removeLink = document.createElement('a');
removeLink.href = 'javascript:void(0)';
removeLink.innerHTML = 'Remove';
removeLink.onclick = function() {
// Remove item
fileList.splice(i, 1);
outputList();
}
node.appendChild(filename);
node.appendChild(removeLink);
nodes.appendChild(node);
});
output.innerHTML = '';
output.appendChild(nodes);
}

<form asp-action="UploadAction" method="post" class="form-horizontal" enctype="multipart/form-data">
<input type="file" multiple>
<input type="submit">
</form>
<div id="fileList"></div>








var formData = new FormData();
var fileList = ;
var fileInput = document.querySelector('input[type="file"]');

fileInput.addEventListener('change', setList);
document.querySelector('form').addEventListener('submit', sendModifiesList);

function setList() {
//Convert the FileList Object to an Array of File Objects
fileList = Array.from(fileInput.files);
outputList();
}


function sendModifiesList(e) {
e.preventDefault();
fileList.forEach(function(file) {
formData.append('fileList', file);
});
console.log("These files will be posted: ", formData.getAll("fileList"));
/*************** EDIT *************************/
// Get the url from the form's action attribute
let url = document.forms[0].action;
let request = new XMLHttpRequest();
// Create a POST request
request.open("POST", url);
// Set up an onload handler to report status
request.onload = function() {
if (request.status == 200) {
console.log("Uploaded!");
} else {
console.log("Error " + request.status + " occurred when trying to upload your file.");
}
};
// Send the form to the server
request.send(formData);
/************ END EDIT ***********************/
};

function outputList() {
var output = document.getElementById('fileList');
var nodes = document.createElement('ul');
fileList.forEach((file, i) => {
var node = document.createElement('li');
var filename = document.createTextNode(file.name);
var removeLink = document.createElement('a');
removeLink.href = 'javascript:void(0)';
removeLink.innerHTML = 'Remove';
removeLink.onclick = function() {
// Remove item
fileList.splice(i, 1);
outputList();
}
node.appendChild(filename);
node.appendChild(removeLink);
nodes.appendChild(node);
});
output.innerHTML = '';
output.appendChild(nodes);
}

<form asp-action="UploadAction" method="post" class="form-horizontal" enctype="multipart/form-data">
<input type="file" multiple>
<input type="submit">
</form>
<div id="fileList"></div>





var formData = new FormData();
var fileList = ;
var fileInput = document.querySelector('input[type="file"]');

fileInput.addEventListener('change', setList);
document.querySelector('form').addEventListener('submit', sendModifiesList);

function setList() {
//Convert the FileList Object to an Array of File Objects
fileList = Array.from(fileInput.files);
outputList();
}


function sendModifiesList(e) {
e.preventDefault();
fileList.forEach(function(file) {
formData.append('fileList', file);
});
console.log("These files will be posted: ", formData.getAll("fileList"));
/*************** EDIT *************************/
// Get the url from the form's action attribute
let url = document.forms[0].action;
let request = new XMLHttpRequest();
// Create a POST request
request.open("POST", url);
// Set up an onload handler to report status
request.onload = function() {
if (request.status == 200) {
console.log("Uploaded!");
} else {
console.log("Error " + request.status + " occurred when trying to upload your file.");
}
};
// Send the form to the server
request.send(formData);
/************ END EDIT ***********************/
};

function outputList() {
var output = document.getElementById('fileList');
var nodes = document.createElement('ul');
fileList.forEach((file, i) => {
var node = document.createElement('li');
var filename = document.createTextNode(file.name);
var removeLink = document.createElement('a');
removeLink.href = 'javascript:void(0)';
removeLink.innerHTML = 'Remove';
removeLink.onclick = function() {
// Remove item
fileList.splice(i, 1);
outputList();
}
node.appendChild(filename);
node.appendChild(removeLink);
nodes.appendChild(node);
});
output.innerHTML = '';
output.appendChild(nodes);
}

<form asp-action="UploadAction" method="post" class="form-horizontal" enctype="multipart/form-data">
<input type="file" multiple>
<input type="submit">
</form>
<div id="fileList"></div>






javascript asp.net-core file-upload asp.net-core-mvc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 6:19









itminus

3,2111320




3,2111320










asked Dec 28 '18 at 3:10









Matt

175




175












  • Yes, I have added.
    – Matt
    Dec 28 '18 at 4:49


















  • Yes, I have added.
    – Matt
    Dec 28 '18 at 4:49
















Yes, I have added.
– Matt
Dec 28 '18 at 4:49




Yes, I have added.
– Matt
Dec 28 '18 at 4:49












1 Answer
1






active

oldest

votes


















0















  1. Your ajax code is sending files with the name of fileList , but your server expects a parameter with a name of parameterIsHere and the Type should be UploadFiles.

  2. It seems that you doesn't send a CSRF token


How to fix :



Approach A :





  1. Server Side: change your action method as below (Note the Type and the name):



    [HttpPost]
    public IActionResult UploadAction(List<IFormFile> fileList)
    {
    // ...
    }



  2. Client Side : add CSRF token and change the name of fields to be fileList:



    function sendModifiesList(e) {
    e.preventDefault();
    fileList.forEach(function(file,idx) {
    formData.append(`fileList`, file); //// name should be `fileList`
    });
    formData.append("__RequestVerificationToken",$("form input[name='__RequestVerificationToken']").val());
    console.log("These files will be posted: ", formData.getAll("fileList"));
    /*************** EDIT *************************/
    // Get the url from the form's action attribute
    let url = document.forms[0].action;
    let request = new XMLHttpRequest();
    // Create a POST request
    request.open("POST", url);
    // Set up an onload handler to report status
    request.onload = function() {
    if (request.status == 200) {
    console.log("Uploaded!");
    } else {
    console.log("Error " + request.status + " occurred when trying to upload your file.");
    }
    };
    // Send the form to the server
    request.send(formData);
    /************ END EDIT ***********************/
    };



Approach B :



if you don't care the name, you could simply use the HttpContext.Request.Form.Files to get the files:



    [HttpPost]
public IActionResult UploadAction()
{
var files= HttpContext.Request.Form.Files;
// ...
}





share|improve this answer





















  • Thanks a lot!!! You're a life saver! :)
    – Matt
    Dec 28 '18 at 6:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953268%2fremove-selected-files-by-clicking-the-remove-link-from-the-input-file-multiple%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0















  1. Your ajax code is sending files with the name of fileList , but your server expects a parameter with a name of parameterIsHere and the Type should be UploadFiles.

  2. It seems that you doesn't send a CSRF token


How to fix :



Approach A :





  1. Server Side: change your action method as below (Note the Type and the name):



    [HttpPost]
    public IActionResult UploadAction(List<IFormFile> fileList)
    {
    // ...
    }



  2. Client Side : add CSRF token and change the name of fields to be fileList:



    function sendModifiesList(e) {
    e.preventDefault();
    fileList.forEach(function(file,idx) {
    formData.append(`fileList`, file); //// name should be `fileList`
    });
    formData.append("__RequestVerificationToken",$("form input[name='__RequestVerificationToken']").val());
    console.log("These files will be posted: ", formData.getAll("fileList"));
    /*************** EDIT *************************/
    // Get the url from the form's action attribute
    let url = document.forms[0].action;
    let request = new XMLHttpRequest();
    // Create a POST request
    request.open("POST", url);
    // Set up an onload handler to report status
    request.onload = function() {
    if (request.status == 200) {
    console.log("Uploaded!");
    } else {
    console.log("Error " + request.status + " occurred when trying to upload your file.");
    }
    };
    // Send the form to the server
    request.send(formData);
    /************ END EDIT ***********************/
    };



Approach B :



if you don't care the name, you could simply use the HttpContext.Request.Form.Files to get the files:



    [HttpPost]
public IActionResult UploadAction()
{
var files= HttpContext.Request.Form.Files;
// ...
}





share|improve this answer





















  • Thanks a lot!!! You're a life saver! :)
    – Matt
    Dec 28 '18 at 6:03
















0















  1. Your ajax code is sending files with the name of fileList , but your server expects a parameter with a name of parameterIsHere and the Type should be UploadFiles.

  2. It seems that you doesn't send a CSRF token


How to fix :



Approach A :





  1. Server Side: change your action method as below (Note the Type and the name):



    [HttpPost]
    public IActionResult UploadAction(List<IFormFile> fileList)
    {
    // ...
    }



  2. Client Side : add CSRF token and change the name of fields to be fileList:



    function sendModifiesList(e) {
    e.preventDefault();
    fileList.forEach(function(file,idx) {
    formData.append(`fileList`, file); //// name should be `fileList`
    });
    formData.append("__RequestVerificationToken",$("form input[name='__RequestVerificationToken']").val());
    console.log("These files will be posted: ", formData.getAll("fileList"));
    /*************** EDIT *************************/
    // Get the url from the form's action attribute
    let url = document.forms[0].action;
    let request = new XMLHttpRequest();
    // Create a POST request
    request.open("POST", url);
    // Set up an onload handler to report status
    request.onload = function() {
    if (request.status == 200) {
    console.log("Uploaded!");
    } else {
    console.log("Error " + request.status + " occurred when trying to upload your file.");
    }
    };
    // Send the form to the server
    request.send(formData);
    /************ END EDIT ***********************/
    };



Approach B :



if you don't care the name, you could simply use the HttpContext.Request.Form.Files to get the files:



    [HttpPost]
public IActionResult UploadAction()
{
var files= HttpContext.Request.Form.Files;
// ...
}





share|improve this answer





















  • Thanks a lot!!! You're a life saver! :)
    – Matt
    Dec 28 '18 at 6:03














0












0








0







  1. Your ajax code is sending files with the name of fileList , but your server expects a parameter with a name of parameterIsHere and the Type should be UploadFiles.

  2. It seems that you doesn't send a CSRF token


How to fix :



Approach A :





  1. Server Side: change your action method as below (Note the Type and the name):



    [HttpPost]
    public IActionResult UploadAction(List<IFormFile> fileList)
    {
    // ...
    }



  2. Client Side : add CSRF token and change the name of fields to be fileList:



    function sendModifiesList(e) {
    e.preventDefault();
    fileList.forEach(function(file,idx) {
    formData.append(`fileList`, file); //// name should be `fileList`
    });
    formData.append("__RequestVerificationToken",$("form input[name='__RequestVerificationToken']").val());
    console.log("These files will be posted: ", formData.getAll("fileList"));
    /*************** EDIT *************************/
    // Get the url from the form's action attribute
    let url = document.forms[0].action;
    let request = new XMLHttpRequest();
    // Create a POST request
    request.open("POST", url);
    // Set up an onload handler to report status
    request.onload = function() {
    if (request.status == 200) {
    console.log("Uploaded!");
    } else {
    console.log("Error " + request.status + " occurred when trying to upload your file.");
    }
    };
    // Send the form to the server
    request.send(formData);
    /************ END EDIT ***********************/
    };



Approach B :



if you don't care the name, you could simply use the HttpContext.Request.Form.Files to get the files:



    [HttpPost]
public IActionResult UploadAction()
{
var files= HttpContext.Request.Form.Files;
// ...
}





share|improve this answer













  1. Your ajax code is sending files with the name of fileList , but your server expects a parameter with a name of parameterIsHere and the Type should be UploadFiles.

  2. It seems that you doesn't send a CSRF token


How to fix :



Approach A :





  1. Server Side: change your action method as below (Note the Type and the name):



    [HttpPost]
    public IActionResult UploadAction(List<IFormFile> fileList)
    {
    // ...
    }



  2. Client Side : add CSRF token and change the name of fields to be fileList:



    function sendModifiesList(e) {
    e.preventDefault();
    fileList.forEach(function(file,idx) {
    formData.append(`fileList`, file); //// name should be `fileList`
    });
    formData.append("__RequestVerificationToken",$("form input[name='__RequestVerificationToken']").val());
    console.log("These files will be posted: ", formData.getAll("fileList"));
    /*************** EDIT *************************/
    // Get the url from the form's action attribute
    let url = document.forms[0].action;
    let request = new XMLHttpRequest();
    // Create a POST request
    request.open("POST", url);
    // Set up an onload handler to report status
    request.onload = function() {
    if (request.status == 200) {
    console.log("Uploaded!");
    } else {
    console.log("Error " + request.status + " occurred when trying to upload your file.");
    }
    };
    // Send the form to the server
    request.send(formData);
    /************ END EDIT ***********************/
    };



Approach B :



if you don't care the name, you could simply use the HttpContext.Request.Form.Files to get the files:



    [HttpPost]
public IActionResult UploadAction()
{
var files= HttpContext.Request.Form.Files;
// ...
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 28 '18 at 5:25









itminus

3,2111320




3,2111320












  • Thanks a lot!!! You're a life saver! :)
    – Matt
    Dec 28 '18 at 6:03


















  • Thanks a lot!!! You're a life saver! :)
    – Matt
    Dec 28 '18 at 6:03
















Thanks a lot!!! You're a life saver! :)
– Matt
Dec 28 '18 at 6:03




Thanks a lot!!! You're a life saver! :)
– Matt
Dec 28 '18 at 6:03


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53953268%2fremove-selected-files-by-clicking-the-remove-link-from-the-input-file-multiple%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

compose and upload a new article using a custom form

How to correct the classpath of spring boot application so that it contains a single, compatible version of...