Reputation: 371
I have a form which contains a textarea and a input of type file. When a file is selected, the textarea will be loaded with file's content. What i want is to able to edit textarea and also to build another file object (or change file's content) which is attached to formData used when the form is submit. In simple terms the php server should receive one file with content from textarea. I think one solution is to change actual real file (from storage) and after that to upload it but i don't want to change the file. Thank you and have a great day.
<form id='form' enctype='multipart/form-data'>
<textarea name='textarea'>
</textarea>
<span class='btn btn-default btn-file'>
<input type='file' name='file">
</span>
</form>
<script>
$("#form").submit(function(event){
var formData = new FormData(this);
$.ajax({
url: '....',
type: 'POST',
data: formData,
success: function (result) {
.....
}
}
</script>
Upvotes: 2
Views: 1190
Reputation: 371
I found the solution:
var file = new File(["file content"], "fileName.txt", {type: "text/plain"});
formData.set('keyForPost', file);
To replace old data file from input, keyForPost should be the input's name attribute.
Upvotes: 2